-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEucDistMatrix.js
61 lines (40 loc) · 1.73 KB
/
EucDistMatrix.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
// Function: EucDistMatrix()
// Authors: Corey Devin Anderson and Kirankumar Batchu
//------------------------------------------------------------------------------//
// Description:
// JavaScript function for calculating pairwise Euclidean distance of row
// vectors in input "Array" or "DenseMatrix" using math.distance
//------------------------------------------------------------------------------//
// Parameters:
// yourMatrix : an r x c "Array" or "DenseMatrix" object.
// outputStyle : string indicating the type of object to output ("Array or DenseMatrix")
// Returns: an nrow x nrow (square) distance matrix.
//------------------------------------------------------------------------------//
// Dependencies:
// math.js
//------------------------------------------------------------------------------//
//------------------------------------------------------------------------------//
// START
function EucDistMatrix(yourMatrix, outputStyle = "Array") {
// Check object type; if "DenseMatrix" convert to "Array"
let yourType = math.typeOf(yourMatrix)
if (yourType == "DenseMatrix") {
yourMatrix = yourMatrix["_data"]
}
let yourDims = math.size(yourMatrix);
let nrow = yourDims[0]; // nrows
let EucDistMat = math.zeros(nrow, nrow); // matrix of zeros to populate
// loop over all i -> j and j <- i comparisons
// use matrix.set([i, j], ...) to replaces ones with
for (i = 0; i < nrow; i++) {
for (j = 0; j < nrow; j++) {
EucDistMat.set([i, j], math.distance(yourMatrix[i], yourMatrix[j]));
}
}
if (outputStyle == "Array") {
EucDistMat = EucDistMat["_data"]
}
return EucDistMat
}
// END
//------------------------------------------------------------------------------//