forked from ipsorakis/GeneralNetworkTools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_modularity.m
60 lines (47 loc) · 1.03 KB
/
get_modularity.m
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 Q = get_modularity(partition,W,directed_flag)
if nargin<3
directed_flag=false;
end
if iscell(partition)
B = group_to_incidence_matrix(partition);
else
B = partition;
end
K = size(B,2);
if ~directed_flag
if K == 1
Q = 0;
else
e = (1/sum(sum(W)))* B' * W * B;
Q = trace(e) - sum(sum(e^2));
end
else
M = sum(sum(W));
N = size(W,1);
Q = 0;
B = B';
k_in = sum(W);
k_out = sum(W,2);
for i=1:N
for j=1:N
if i~=j
k_i_in = k_in(i);
k_j_out = k_out(j);
delta_ij = logical(B(:,i)'*B(:,j));
if delta_ij
Q = Q + (W(i,j) - (k_i_in*k_j_out/M));
end
end
end
end
Q = Q / M;
end
end
function B = group_to_incidence_matrix(groups)
K = length(groups);
N = length(cat(2,groups{:}));
B = zeros(N,K);
for i=1:K
B(groups{i},i) = 1;
end
end