forked from aiavenak/matlab_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcn_group_average.m
76 lines (75 loc) · 2.71 KB
/
fcn_group_average.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
71
72
73
74
75
76
function G = fcn_group_average(A,dist,hemiid)
%FCN_GROUP_AVERAGE build group-average matrix
%
% G = FCN_GROUP_AVERAGE(A,DIST) takes as input a WEIGHTED "stack" of
% connectivity matrices, A, with dimensions [N x N x SUBJECT] where N is
% number of nodes and SUBJECT is the number of matrices in the stack. The
% matrices MUST be weighted, and ideally with continuous weights (e.g.
% fractional anisotropy rather than streamline count). The second input
% is DIST, which is a pairwise distance matrix (i.e. DIST(i,j) is the
% Euclidean distance between nodes i and j). The final input is an [N x
% 1] vector, HEMIID, which labels nodes as in the left (1) or right (2)
% hemisphere.
%
% This function estimates the average edge length distribution and builds
% a group-averaged connectivity matrix that approximates this
% distribution with density equal to the mean density across subjects.
%
% The algorithm works as follows:
% 1. Estimate the cumulative edge length distrbiution.
% 2. Divide the distribution into M length bins, one for each edge that
% will be added to the group-average matrix.
% 3. Within each bin, select the most edge that is most consistently
% expressed across subjects, breaking ties according to average edge
% weight (which is why A must be weighted).
%
% The algorithm works separately on within/between hemisphere links.
%
% Inputs:
% A, stack of connectivity matrices
% DIST, pairwise distance matrix
%
% Outputs: G, binary group-average matrix
%
% Richard Betzel, Indiana University, 2015
% Updated by Andrea A-K, 2016
[n,~,nsub] = size(A);
C = sum(A > 0,3);
W = sum(A,3)./C;
Grp = zeros(n,n,2);
for j = 1:2
if j == 1
d = +(hemiid == 1)*(hemiid' == 2);
d = d | d';
else
d = +(hemiid == 1)*(hemiid' == 1) | +(hemiid == 2)*(hemiid' == 2);
d = d | d';
end
D = nonzeros(bsxfun(@times,(A > 0),dist.*triu(d)));
M = round(length(D))/nsub;
dist_hemi = dist.*d;
[x,y] = ecdf(D);
x = round(x.*M);
G = zeros(n);
for i = 1:M
ind = (x >= (i - 1)) & (x < i);
yy = y(ind);
mask = dist_hemi >= min(yy) & dist_hemi <= max(yy);
[u,v] = find(triu(mask,1));
indx = (v - 1)*n + u;
c = C(indx);
w = W(indx);
zz = sum(c == max(c));
if zz == 1
[~,indmax] = max(c);
G(indx(indmax)) = 1;
else
aa = find(c == max(c));
ww = w(aa);
[~,indsort] = sort(ww,'descend');
G(indx(aa(indsort(1)))) = 1;
end
end
Grp(:,:,j) = G;
end
G = sum(Grp,3); G = G + G';