-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mahalanobis_helpers.js
110 lines (84 loc) · 2.92 KB
/
Mahalanobis_helpers.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Helper functions for Mahalanobis1.js and Mahalanobis2.js
//------------------------------------------------------------------------------
// START
function getColumn(yourMatrix, colNumber){
let outColumn = [];
let nrows = yourMatrix.length;
for (i = 0; i < nrows; i++) {
let rowTemp = yourMatrix[i];
let num = rowTemp[colNumber];
outColumn.push(num);
}
return outColumn;
}
// END
// console.log(getColumn(testMatrixX, 0))
//------------------------------------------------------------------------------
// START
function arrayClean(a) {
let aClean = a.filter(function(x){if (x != null) {return x}});
return (aClean);
}
// END
// console.log(arrayClean(getColumn(testMatrixX, 1)));
//------------------------------------------------------------------------------
// START
function arrayMean(a) {
let sum = 0;
a.forEach(function(element){sum = sum + element});
let n = a.length;
return (sum / n);
}
// END
// console.log(arrayMean(arrayClean(getColumn(testMatrix, 0))));
//------------------------------------------------------------------------------
// Now use getColumn() and arrayClean(), with arrayMean() to get the mean of
// each column in the Array.
// START
function columnMeans(yourMatrix, axis = "columns") {
let blankArray = []
if (axis == "columns") {
let nCols = yourMatrix.length;
for (k = 0; k < nCols; k++) {
blankArray.push(arrayMean(arrayClean(yourMatrix[k])));
}
return (blankArray);
} else if (axis == "rows") {
let nCols = yourMatrix[0].length;
for (k = 0; k < nCols; k++) {
blankArray.push(arrayMean(arrayClean(getColumn(yourMatrix, k))));
}
return (blankArray);
}
}
// END
// console.log(columnMeans(testMatrix_10))
//------------------------------------------------------------------------------
// START
function subtractArrays(a, b) {
let c = a.map((x, i) => {if (x == null || b == null) {return null} else {return (x - b[i])}});
return c;
}
// END
//------------------------------------------------------------------------------
// START
function multiplyArrays(a, b) {
let c = a.map((x, i) => {if (x == null || b[i] == null) {return null} else {return (x * b[i])}});
return c;
}
// END
//------------------------------------------------------------------------------
// START
function sumArray(a) {
let sum = 0;
a.forEach(element => sum = element + sum);
return sum;
}
// END
// console.log(sumArray(getColumn(testMatrix, 0)));
// console.log(subtractArrays(testMatrix[0], testMatrix[1]))
// console.log(math.subtract(testMatrix[0], testMatrix[1]))
// console.log(multiplyArrays([2, 4, 6], [1, 3, 5]));
// console.log(addArrays([2, 4, 6], [1, 3, 5]));
// console.log(math.zeros(3, 3))
// console.log(math.zeros(3, 3)["_data"])