-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat.cpp
47 lines (41 loc) · 856 Bytes
/
stat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* stat.cpp
*
* Created on: Apr 28, 2010
* Author: Vincent
*/
#include "stat.h"
#include <cmath>
qint32 minIndexOf(const QList<double> &list) {
qint32 minIndex = 0;
for (qint32 i = 0; i < list.size(); ++i) {
if (list[minIndex] > list[i]) {
minIndex = i;
}
}
return minIndex;
}
double median(QList<double> data) {
qSort(data);
qint32 n = data.size() / 2;
if (data.size() % 2 != 0 && n != 0) {
return (data[n] + data[n + 1]) / 2.0;
} else {
return data[n];
}
}
double mean(const QList<double> &data) {
double sum = 0.0;
for (qint32 i = 0; i < data.size(); ++i) {
sum += data[i];
}
return sum / data.size();
}
double mad(const QList<double> &data) {
double med = median(data);
QList<double> absDiff;
for (qint32 i = 0; i < data.size(); ++i) {
absDiff << std::abs(data[i] - med);
}
return median(absDiff);
}