forked from ilarinieminen/SOM-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
som_clget.m
115 lines (101 loc) · 3.34 KB
/
som_clget.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function a = som_clget(sC, mode, ind)
%SOM_CLGET Get properties of specified clusters.
%
% a = som_clget(sC, mode, ind)
%
% inds = som_clget(sC,'dinds',20);
% col = som_clget(sC,'depth',[1 2 3 20 54]);
%
% Input and output arguments:
% sC (struct) clustering struct
% mode (string) what kind of property is requested
% 'binds' (a union over) indeces of base clusters
% belonging to the specified cluster(s)
% 'dinds' (a union over) indeces of the data vectors
% belonging to the specified cluster(s)
% 'dlen' number of data vectors belonging
% to each of the specified cluster(s)
% 'depth' depths of the specified clusters
% (depth of the root cluster is 0,
% depth of its children are 1, etc.)
% 'child' (a union over) children clusters
% of specified cluster(s), including
% the clusters themselves
% 'base' base partitioning based on given
% clusters
% ind (vector) indeces of the clusters
%
% a (vector) the answer
%
% See also SOM_CLSTRUCT, SOM_CLPLOT.
% Copyright (c) 2000 by the SOM toolbox programming team.
% Contributed to SOM Toolbox on XXX by Juha Vesanto
% http://www.cis.hut.fi/projects/somtoolbox/
% Version 2.0beta juuso 180800
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clen = size(sC.tree,1)+1;
switch mode,
case 'binds',
a = [];
for i=1:length(ind), a = [a, getbaseinds(sC.tree,ind(i))]; end
a = unique(a);
case 'dinds',
b = [];
for i=1:length(ind), b = [b, getbaseinds(sC.tree,ind(i))]; end
b = unique(b);
a = zeros(length(sC.base),1);
for i=1:length(b), a(find(sC.base==b(i)))=1; end
a = find(a);
case 'dlen',
a = zeros(length(ind),1);
for i=1:length(ind),
b = getbaseinds(sC.tree,ind(i));
for j=1:length(b), a(i) = a(i) + sum(sC.base==b(j)); end
end
case 'depth',
a = getdepth(sC.tree);
a = a(ind);
case 'child',
a = getchildren(sC.tree,ind);
case 'base',
a = sC.base*0;
ind = -sort(-ind);
for i=1:length(ind), a(som_clget(sC,'dinds',ind(i))) = ind(i); end
end
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ch = getchildren(Z,ind)
clen = size(Z,1)+1;
ch = ind; cho = ind;
while any(cho),
i = cho(1); cho = cho(2:end);
j = Z(i-clen,1); k = Z(i-clen,2);
if j>clen, cho(end+1) = j; end
if k>clen, cho(end+1) = k; end
ch(end+1) = j; ch(end+1) = k;
end
return;
function binds = getbaseinds(Z,ind)
clen = size(Z,1)+1;
binds = ind;
while binds(1)>clen,
i = binds(1);
binds = binds(2:end);
j = Z(i-clen,1); k = Z(i-clen,2);
if j>clen, binds = [j binds]; else binds(end+1) = j; end
if k>clen, binds = [k binds]; else binds(end+1) = k; end
end
return;
function depth = getdepth(Z)
clen = size(Z,1)+1;
depth = zeros(2*clen-1,1);
ch = 2*clen-1; % active nodes
while any(ch),
c = ch(1); ch = ch(2:end);
if c>clen && isfinite(Z(c-clen,3)),
chc = Z(c-clen,1:2); % children of c
depth(chc) = depth(c) + 1; % or +(ind==chc(1))
ch = [ch, chc];
end
end
return;