-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathLEiDA_Cluster.m
68 lines (58 loc) · 1.9 KB
/
LEiDA_Cluster.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
function LEiDA_Cluster
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% LEADING EIGENVECTOR DYNAMICS ANALYSIS
%
% This function computes a k-means clustering algorith to the eigenvectors
%
% - Loads all the eigenvectors saved in LEiDA_data
% - Computes k-means clustering
% - Calculates the Dunn's score
% - Selects the optimal solution
%
% - Saves the optimal solution into LEiDA_Clusters.mat
% Clusters =
% IDX: Cluster index for each observation
% C: Cluster centroids
% D: Distance from each observation to every centroid
% SUMD: Within-cluster sums of point-to-centroid distances
%
% Written by
% Joana Cabral [email protected]
% Paulo Marques [email protected]
% Last edited May 2016
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load LEiDA_data Leading_Eig
N_sub=size(Leading_Eig,1);
% Generate vector X concatenating all eigenvectors from all subjects and
% time points, where rows are observations and collumns are variables.
X=[];
for s=1:N_sub
X=cat(1,X,squeeze(Leading_Eig(s,:,:)));
end
clear Leading_Eig
%Kmeans clustering
maxk=20;
% opt= statset('UseParallel',1); %,'UseSubstreams',1);
% The options may vary according to the Matlab version
Kmeans_results=cell(1,20);
parfor k=2:maxk
disp(['Calculating for ' num2str(k) 'clusters'])
[IDX, C, SUMD, D]=kmeans(X,k,'Distance','cityblock','Replicates',20,'Display','final'); %,'Options',opt);
Kmeans_results{k}.IDX=IDX;
Kmeans_results{k}.C=C;
Kmeans_results{k}.SUMD=SUMD;
Kmeans_results{k}.D=D;
end
%Evaluate Clustering performance
distM_fcd=squareform(pdist(X,'cityblock'));
dunn_score=zeros(maxk,1);
for j=2:maxk
dunn_score(j)=dunns(j,distM_fcd,Kmeans_results{j}.IDX);
disp(['Performance for ' num2str(j) 'clusters'])
end
[~,ind_max]=max(dunn_score);
disp(['Best clustering solution: ' num2str(ind_max) ' clusters']);
Clusters= Kmeans_results{ind_max};
save('LEiDA_Clusters','Clusters')