wolffd@0: function a = som_clget(sC, mode, ind) wolffd@0: wolffd@0: %SOM_CLGET Get properties of specified clusters. wolffd@0: % wolffd@0: % a = som_clget(sC, mode, ind) wolffd@0: % wolffd@0: % inds = som_clget(sC,'dinds',20); wolffd@0: % col = som_clget(sC,'depth',[1 2 3 20 54]); wolffd@0: % wolffd@0: % Input and output arguments: wolffd@0: % sC (struct) clustering struct wolffd@0: % mode (string) what kind of property is requested wolffd@0: % 'binds' (a union over) indeces of base clusters wolffd@0: % belonging to the specified cluster(s) wolffd@0: % 'dinds' (a union over) indeces of the data vectors wolffd@0: % belonging to the specified cluster(s) wolffd@0: % 'dlen' number of data vectors belonging wolffd@0: % to each of the specified cluster(s) wolffd@0: % 'depth' depths of the specified clusters wolffd@0: % (depth of the root cluster is 0, wolffd@0: % depth of its children are 1, etc.) wolffd@0: % 'child' (a union over) children clusters wolffd@0: % of specified cluster(s), including wolffd@0: % the clusters themselves wolffd@0: % 'base' base partitioning based on given wolffd@0: % clusters wolffd@0: % ind (vector) indeces of the clusters wolffd@0: % wolffd@0: % a (vector) the answer wolffd@0: % wolffd@0: % See also SOM_CLSTRUCT, SOM_CLPLOT. wolffd@0: wolffd@0: % Copyright (c) 2000 by the SOM toolbox programming team. wolffd@0: % Contributed to SOM Toolbox on XXX by Juha Vesanto wolffd@0: % http://www.cis.hut.fi/projects/somtoolbox/ wolffd@0: wolffd@0: % Version 2.0beta juuso 180800 wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: clen = size(sC.tree,1)+1; wolffd@0: wolffd@0: switch mode, wolffd@0: case 'binds', wolffd@0: a = []; wolffd@0: for i=1:length(ind), a = [a, getbaseinds(sC.tree,ind(i))]; end wolffd@0: a = unique(a); wolffd@0: case 'dinds', wolffd@0: b = []; wolffd@0: for i=1:length(ind), b = [b, getbaseinds(sC.tree,ind(i))]; end wolffd@0: b = unique(b); wolffd@0: a = zeros(length(sC.base),1); wolffd@0: for i=1:length(b), a(find(sC.base==b(i)))=1; end wolffd@0: a = find(a); wolffd@0: case 'dlen', wolffd@0: a = zeros(length(ind),1); wolffd@0: for i=1:length(ind), wolffd@0: b = getbaseinds(sC.tree,ind(i)); wolffd@0: for j=1:length(b), a(i) = a(i) + sum(sC.base==b(j)); end wolffd@0: end wolffd@0: case 'depth', wolffd@0: a = getdepth(sC.tree); wolffd@0: a = a(ind); wolffd@0: case 'child', wolffd@0: a = getchildren(sC.tree,ind); wolffd@0: case 'base', wolffd@0: a = sC.base*0; wolffd@0: ind = -sort(-ind); wolffd@0: for i=1:length(ind), a(som_clget(sC,'dinds',ind(i))) = ind(i); end wolffd@0: end wolffd@0: wolffd@0: return; wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: function ch = getchildren(Z,ind) wolffd@0: wolffd@0: clen = size(Z,1)+1; wolffd@0: ch = ind; cho = ind; wolffd@0: while any(cho), wolffd@0: i = cho(1); cho = cho(2:end); wolffd@0: j = Z(i-clen,1); k = Z(i-clen,2); wolffd@0: if j>clen, cho(end+1) = j; end wolffd@0: if k>clen, cho(end+1) = k; end wolffd@0: ch(end+1) = j; ch(end+1) = k; wolffd@0: end wolffd@0: return; wolffd@0: wolffd@0: function binds = getbaseinds(Z,ind) wolffd@0: wolffd@0: clen = size(Z,1)+1; wolffd@0: binds = ind; wolffd@0: while binds(1)>clen, wolffd@0: i = binds(1); wolffd@0: binds = binds(2:end); wolffd@0: j = Z(i-clen,1); k = Z(i-clen,2); wolffd@0: if j>clen, binds = [j binds]; else binds(end+1) = j; end wolffd@0: if k>clen, binds = [k binds]; else binds(end+1) = k; end wolffd@0: end wolffd@0: return; wolffd@0: wolffd@0: function depth = getdepth(Z) wolffd@0: wolffd@0: clen = size(Z,1)+1; wolffd@0: depth = zeros(2*clen-1,1); wolffd@0: ch = 2*clen-1; % active nodes wolffd@0: while any(ch), wolffd@0: c = ch(1); ch = ch(2:end); wolffd@0: if c>clen & isfinite(Z(c-clen,3)), wolffd@0: chc = Z(c-clen,1:2); % children of c wolffd@0: depth(chc) = depth(c) + 1; % or +(ind==chc(1)) wolffd@0: ch = [ch, chc]; wolffd@0: end wolffd@0: end wolffd@0: return;