-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_degree_zscore.cpp
44 lines (35 loc) · 1.12 KB
/
module_degree_zscore.cpp
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
#include <gsl/gsl_math.h>
#include "bct.h"
/*
* Computes z-score for a binary graph and its corresponding community
* structure. For a directed graph, computes out-neighbor z-score.
*/
VECTOR_T* BCT_NAMESPACE::module_degree_zscore(const MATRIX_T* A, const VECTOR_T* Ci) {
if (safe_mode) check_status(A, SQUARE | BINARY, "module_degree_zscore");
// n=length(A);
int n = length(A);
// Z=zeros(n,1);
VECTOR_T* Z = zeros_vector(n);
// for i=1:max(Ci)
for (int i = 1; i <= (int)max(Ci); i++) {
// Koi=sum(A(Ci==i,Ci==i),2);
VECTOR_T* Ci_eq_i = compare_elements(Ci, fp_equal, (FP_T)i);
MATRIX_T* A_idx = logical_index(A, Ci_eq_i, Ci_eq_i);
VECTOR_T* Koi = sum(A_idx, 2);
MATRIX_ID(free)(A_idx);
// Z(Ci==i)=(Koi-mean(Koi))./std(Koi);
FP_T std_Koi = MATLAB_NAMESPACE::std(Koi);
VECTOR_ID(add_constant)(Koi, -mean(Koi));
VECTOR_ID(scale)(Koi, 1.0 / std_Koi);
logical_index_assign(Z, Ci_eq_i, Koi);
VECTOR_ID(free)(Ci_eq_i);
VECTOR_ID(free)(Koi);
}
// Z(isnan(Z))=0;
for (int i = 0; i < (int)Z->size; i++) {
if (gsl_isnan(VECTOR_ID(get)(Z, i)) == 1) {
VECTOR_ID(set)(Z, i, 0.0);
}
}
return Z;
}