-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatrices2svdscores.m
56 lines (52 loc) · 1.64 KB
/
matrices2svdscores.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
function [scores subjects groups_reordered varscores] = matrices2svdscores(m3d,groups,nodes,PCA)
%matrices2svdscores plots svd scores for 2D matrices stacked along 3rd
%dimension
%
% [scores subjects groups_reordered] = matrices2svdscores(m3d,groups)
%
% INPUTS:
% - m3d is your subject_array_3D
% - group is a 1D numerical vector of group assignments (i.e. 1, 2, 3,
% etc.) for each subject. It should correspond to the position of
% subjects in subject_array_3D
% - nodes is either 0 (to include all edge weights) or 1 (for just node
% strengths)
% - pca is optional argument: set to 1 if you want to run pca instead
%
% EXAMPLES:
% matrices2svdscores(subject_array_3D,groups,0)
% matrices2svdscores(subject_array_3D,groups,0,1)
%
% outputs will be arranged in order of ascending scores
%
% -David Grayson 2014
%% convert 2D mats stacked on 3rd dim to 1D mats stacked on 2nd dim
mlen=size(m3d,1); %2D matrix size
slen=size(m3d,3); %# of subjects
I=find(triu(ones(mlen,mlen),1)); %find upper triangular indices, excluding
%diagonal
for s=1:slen
s2d=m3d(:,:,s);
if nodes == 1
m2d(:,s)=strengths_und(s2d);
else
m2d(:,s)=s2d(I);
end
end
%% get svd scores for each subject
if exist('PCA','var') && PCA
[V,S,U] = pca(m2d);
else
[U,S,V] = svd(m2d);
end
scores=(V(:,2));
varscores=(U(:,2));
%% reorganize by group assignments
[scores subjects]=sort(scores);
groups_reordered=groups(subjects);
%% plot
scatter(1:length(scores),scores,50,groups_reordered,'filled');
title('subject scores');
figure();
scatter(1:length(varscores),varscores,50,'filled');
title('variable scores');