-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatching.m
70 lines (52 loc) · 2.37 KB
/
matching.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
61
62
63
64
65
66
67
68
69
70
function m = matching(A,alt)
% Computes the matching index m for an adjacency matrix A. The matching
% index is the proportion of a node pairs total connections thats are to
% the same node.
% The alternative way is to define it is out of all the neighbours of a
% pair of nodes, what proportion of them are in common. Set alt = 1 to get
% this definition
if nargin < 2
alt = 0;
end
% Make the network binary
A = A > 0;
% Get the number of nodes n
n = length(A);
% Calculate the neighbours for each pair of nodes
%nei = (A*A).*~eye(n);
nei = (A*A);
% Get the degree of each node, followed by the summed degree of each pair
% of nodes
deg = sum(A);
%degsum = (deg+deg').*~eye(n);
degsum = (deg+deg');
% Originally I calculated the identity matrix and used that to set the
% diagonal to zero. This meant in subsequent calculations, a nodes "self"
% matching index would be 0. However, it really doesn't matter what it is
% as we don't tend to use that value anyway. So to save some compute time,
% we avoid calculating this matrix
% Compute the matching index.
% To avoid dividing by zero we include the term (degsum<=2 & nei~=1).
% If two nodes have no connections, their matching index will be 0/0
% which equals nan. We can search and replace nans using 'isnan'
% however for very large networks, searching for these nans takes a
% surprising amount of time. To work around this, the section
% "(degmat_sum<=2 & nei~=1)" takes the value of 1 when two nodes have
% one connection or less each and don't have exactly one neighbor. The
% reason "degmat_sum<=2" is used is because if two nodes each have a
% degree of one but no shared neighbors, this means those two nodes are
% connected to each other (and technically share no neighbors). In this
% case the equation "degmat_sum - (A.*2)" equals zero (as the summed
% degree is cancelled by their shared connection) and could cause the
% whole equation to fail. The "& nei~=1" catches a case where two nodes
% are only connected to the same node (i.e., they share one neighbor).
% If this was not there (i.e., only "degmat_sum<=2" was used) then an
% erroneous value of one will be added to the denominator, giving an
% incorrect result.
if alt == 0
m = (nei*2)./( (degsum<=2 & nei~=1) + (degsum-(A.*2)) );
elseif alt == 1
m = (nei)./( (degsum<=2 & nei~=1) + (degsum-(A.*2)-nei) );
end
% Remove ones from the diagonal
m = m.*~eye(n);