-
Notifications
You must be signed in to change notification settings - Fork 2
/
assortativity.m
36 lines (33 loc) · 1.13 KB
/
assortativity.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
function r = assortativity(CIJ)
%ASSORTATIVITY Assortativity coefficient
%
% r = assortativity(CIJ,flag);
%
% The assortativity coefficient is a correlation coefficient between the
% degrees of all nodes on two opposite ends of a link. A positive
% assortativity coefficient indicates that nodes tend to link to other
% nodes with the same or similar degree.
%
% Inputs: CIJ, binary directed/undirected connection matrix
% flag, 1 = directed graph; 0 = non-directed graph
%
% Outputs: r, assortativity
%
% Notes: The function accepts weighted networks, but all connection
% weights are ignored. The main diagonal should be empty.
%
% Reference: Newman (2002) Phys Rev Lett 89:208701.
%
%
% Olaf Sporns, Indiana University, 2007/2008
% Vassilis Tsiaras, University of Crete, 2009
CIJ = 1*logical(CIJ);
[deg] = sum(CIJ);
[i,j] = find(triu(CIJ,1)>0);
K = length(i); % node pairs
for k=1:K
degi(k) = deg(i(k));
degj(k) = deg(j(k));
end
% compute assortativity
r = (sum(degi.*degj)/K - (sum(0.5*(degi+degj))/K)^2)/(sum(0.5*(degi.^2+degj.^2))/K - (sum(0.5*(degi+degj))/K)^2);