-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinalClust.m
57 lines (52 loc) · 1.62 KB
/
finalClust.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
function cl = finalClust(cl,distK,rho,NCLUST)
% perform final processing on the merged sub-clusters (in cases where the K value is too small)
num = length(unique(cl));
if num<=NCLUST
return;
end
ND = length(cl);
numcl = unique(cl);
nClu = zeros(num,1);
clu = zeros(num,ND);
for i=1:num
item = find(cl==numcl(i));
nClu(i) = length(item);
clu(i,1:nClu(i)) = item(:);
end
[~,index] = sort(nClu);
count = num-NCLUST;
while count>0
idx = index(count);
clu_idx = clu(idx,1:nClu(idx)); % find all elements of the sub-cluster to be merged
clu_idx_exp = distK(clu_idx,:); % find the extension of all elements of the sub-cluster to be merged (KNN)
clu_idx_exp = clu_idx_exp(:);
clu_cl = unique(cl(clu_idx_exp));
mergIdx = 0;
mergValue = 0;
for j=1:num
if j==idx
continue;
end
if ismember(numcl(j),clu_cl)
idxI = clu_idx;
idxJ = clu(j,1:nClu(j));
PavgI = mean(rho(idxI));
PavgJ = mean(rho(idxJ));
stdI = max(std(rho(idxI)),1e-6); % when there is only one sample in the cluster, the variance is zero.
stdJ = std(rho(idxJ));
stdIJ = std([rho(idxI)' rho(idxJ)']);
merg = min(PavgI,PavgJ)*min(stdI,stdJ)/max(PavgI,PavgJ)/max(stdI,stdJ);
merg = sqrt(merg)/stdIJ;
if merg>mergValue
mergValue = merg;
mergIdx = j;
end
end
end
if mergIdx>0
ci = cl(clu_idx(1));
cl(cl==ci) = numcl(mergIdx);
end
count=count-1;
end
end