This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathis_missing.hhs
60 lines (50 loc) · 1.68 KB
/
is_missing.hhs
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
/**
* @author Jianan Lin (林家南)
* @param input - an array, matrix, tensor that you want to find missing element
* @returns - a matrix or tensor with same size, element is 1 or 0 depending on missing or not
* A missing element is NaN, null, '', or undefined. Notice [] and {} are not missing.
*
*/
function is_missing(input) {
*import math: ndim
*import math: deep_copy
// argument check
if (arguments.length === 0) {
throw new Error('Exception occurred in is_missing - no argument given');
}
if (arguments.length > 1) {
throw new Error('Exception occurred in is_missing - wrong argument number');
}
if (!(Array.isArray(input)) && !(input instanceof Mat) && !(input instanceof Tensor)) {
throw new Error('Exception occurred in is_missing - input must be an array, matrix or tensor');
}
let in_type = input instanceof Mat || input instanceof Tensor;
let raw_in = in_type ? input.clone().val : deep_copy(input);
let result = deep_copy(raw_in);
ismissing_helper(raw_in, result);
if (ndim(result) <= 2) {
return mat(result);
}
else {
return new Tensor(result);
}
function ismissing_helper(raw_in, result) {
if (ndim(raw_in) === 1) {
for (let i = 0; i < raw_in.length; i++) {
if (raw_in[i] || raw_in[i] === 0) {
result[i] = 0;
}
else {
result[i] = 1
}
}
return;
}
else {
for (let i = 0; i < raw_in.length; i++) {
ismissing_helper(raw_in[i], result[i]);
}
return;
}
}
}