-
Notifications
You must be signed in to change notification settings - Fork 0
/
distEuclidean2.js
33 lines (22 loc) · 1.11 KB
/
distEuclidean2.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
// Function: distEuclidean2()
// Authors: Corey Devin Anderson and Kirankumar Batchu
//----------------------------------------------------------------------------
// Description:
// Calculates the Euclidean distance between 1D Arrays of numbers. If one or
// more values are null that component is ignored in the calculation.
//----------------------------------------------------------------------------
// Parameters:
// a, b : 1D Arrays of numbers to be compared.
// Returns: Euclidean distance between arrays (a, b).
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Function: distEuclidean2()
// START
function distEuclidean2(a, b) {
let squares = a.map(function(x, i){if (x != null && b[i] != null) {return (x - b[i]) ** 2} else {return null}});
let sum = 0;
squares.forEach(element => sum = sum + element) // Sum the squares
return(sum ** 0.5) // return the square root of the sums of squares
}
// END
//----------------------------------------------------------------------------