-
Notifications
You must be signed in to change notification settings - Fork 0
/
distEuclidean.js
36 lines (24 loc) · 1.17 KB
/
distEuclidean.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
// Function: distEuclidean()
// Authors: Corey Devin Anderson and Kirankumar Batchu
//----------------------------------------------------------------------------
// Description:
// Calculates the Euclidean distance between 1D Arrays of numbers\
//----------------------------------------------------------------------------
// Parameters:
// a, b : 1D Arrays of numbers to be compared.
// Returns: Euclidean distance between arrays (a, b).
// Dependencies: none.
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Function: distEuclidean()
// START
function distEuclidean(a, b) {
let squares = a.map((x, i) => (x - b[i]) ** 2); // Take the difference between corresponding components
// and square it.
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
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------