Daniel@0: function [V,I]=som_divide(sMap, D, inds, mode) Daniel@0: Daniel@0: %SOM_DIVIDE Divides a dataset according to a given map. Daniel@0: % Daniel@0: % [V,I]=som_divide(sMap, sData, [inds], [mode]) Daniel@0: % Daniel@0: % ARGUMENTS ([]'s are optional) Daniel@0: % Daniel@0: % sMap (struct or matrix) map struct or codebook (size munits x dim) Daniel@0: % sData (struct or matrix) data struct or matrix (size N x dim ) Daniel@0: % [inds] From which map units should the local data sets Daniel@0: % be constructed. Interpretation depends on mode Daniel@0: % argument. By default [1:munits]. Daniel@0: % 'class': (vector) munits x 1 matrix of class numbers Daniel@0: % 'index': (vector) K x 1 vector of map node indexes Daniel@0: % 'index': (matrix) K x k matrix of map node subscripts Daniel@0: % [mode] (string) 'index' or 'class', if inds is a vector of length Daniel@0: % munits, default is 'class', otherwise 'index'. Daniel@0: % RETURNS Daniel@0: % Daniel@0: % If mode == 'index' Daniel@0: % V (matrix) data vectors hitting the specified nodes (size K x dim) Daniel@0: % I (vector) corresponding data row indexes (size K x 1) Daniel@0: % Daniel@0: % If mode == 'class' (this can be used after using som_select) Daniel@0: % V (cell array) V{K} includes vectors whose BMU has class number Daniel@0: % K in the input matrix 'coord'. Note that Daniel@0: % values of K below 1 are ignored. Daniel@0: % I (cell array) corresponding data indexes in the cell array Daniel@0: % Daniel@0: % NOTE: if the same node is specified multiple times, only one Daniel@0: % set of hits is returned. Daniel@0: % Daniel@0: % See also SOM_BMU, SOM_HITS, SOM_SELECT. Daniel@0: Daniel@0: % Version 1.0beta 260997 Johan Daniel@0: % Version 2.0beta 230300 juuso Daniel@0: Daniel@0: % Contributed to SOM Toolbox vs2, Mar 23rd, 2000 by Juha Vesanto Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: %%%% Init & Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: error(nargchk(0, 4, nargin)) % check if no. of input args is correct Daniel@0: Daniel@0: % map Daniel@0: if isstruct(sMap), Daniel@0: msize = sMap.topol.msize; Daniel@0: dim = size(sMap.codebook,2); Daniel@0: else Daniel@0: msize = [size(sMap,1) 1]; Daniel@0: dim = size(sMap,2); Daniel@0: end Daniel@0: munits = prod(msize); Daniel@0: Daniel@0: % data Daniel@0: if isstruct(D), D=D.data; end Daniel@0: Daniel@0: % inds Daniel@0: if nargin<3, inds = 1:munits; end Daniel@0: isvec = prod(size(inds))==length(inds); Daniel@0: Daniel@0: % mode Daniel@0: if nargin<4, Daniel@0: if isvec & length(inds)==munits, Daniel@0: mode = 'class'; Daniel@0: else Daniel@0: mode = 'index'; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: %%% Action & Build output according to the mode string output Daniel@0: Daniel@0: if ~isvec, inds = som_sub2ind(msize,inds); end Daniel@0: Daniel@0: bmus=som_bmus(sMap,D); Daniel@0: Daniel@0: switch mode Daniel@0: case 'index' Daniel@0: I=find(ismember(bmus,inds)); Daniel@0: V=D(I,:); Daniel@0: case 'class' Daniel@0: K=max(inds); % classes Daniel@0: V = cell(K,1); Daniel@0: I = cell(K,1); Daniel@0: for i=1:K, Daniel@0: N_ind=find(inds == i); % indexes of the units of class i Daniel@0: I{i}=find(ismember(bmus,N_ind)); % data indexes Daniel@0: V{i}=D(I{i},:); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: