-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistEuclidean3.js
60 lines (44 loc) · 2.18 KB
/
distEuclidean3.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
// Function: distEuclidean3()
// Authors: Corey Devin Anderson and Kirankumar Batchu
//----------------------------------------------------------------------------
// Description:
// Calculates the Euclidean distance between 1D Arrays of numbers. If one or
// more values are null, a default value is returned.
//----------------------------------------------------------------------------
// Parameters:
// a, b : 1D Arrays of numbers to be compared.
// nullValuesReturn : Value that is returned if either array contains null
// values. Default is -3.
// Returns: Euclidean distance between arrays (a, b).
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// START
function distEuclidean3(a, b, nullValuesReturn = -3) {
let testNull = (element) => element == null;
aTest = a.some(testNull);
bTest = b.some(testNull);
let sum = 0;
if (aTest == false && bTest == false){
let squares = a.map((x, i) => (x - b[i]) ** 2);
squares.forEach(element => sum = sum + element); // Sum the squares
sum = sum ** 0.5;
} else {
sum = nullValuesReturn;
}
return(sum);
}
// END
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Example data:
// Data are percentile ranks for five indicators from SVI 2018 and diabetes:
const FIPS_01001 = [0.1850, 0.5401, 0.6336, 0.4397, 0.2745, 0.7074];
const FIPS_01003 = [0.6428, 0.2239, 0.5158, 0.3209, 0.3121, 0.4925];
const FIPS_01005 = [0.4893, 0.9631, 0.8965, 0.9701, 0.9217, 0.9799];
// with nulls
const FIPS_01001x = [0.1850, null, 0.6336, 0.4397, 0.2745, 0.7074];
const FIPS_01003x = [0.6428, 0.2239, 0.5158, null, 0.3121, 0.4925];
console.log(distEuclidean3(FIPS_01001, FIPS_01003)); // should return 0.6206040041765764
console.log(distEuclidean3(FIPS_01001x, FIPS_01003x)); // should return -3
console.log(distEuclidean3(FIPS_01001, FIPS_01003x)); // should return -3
console.log(distEuclidean3(FIPS_01001x, FIPS_01003)); // should return -3