annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_divide.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [V,I]=som_divide(sMap, D, inds, mode)
wolffd@0 2
wolffd@0 3 %SOM_DIVIDE Divides a dataset according to a given map.
wolffd@0 4 %
wolffd@0 5 % [V,I]=som_divide(sMap, sData, [inds], [mode])
wolffd@0 6 %
wolffd@0 7 % ARGUMENTS ([]'s are optional)
wolffd@0 8 %
wolffd@0 9 % sMap (struct or matrix) map struct or codebook (size munits x dim)
wolffd@0 10 % sData (struct or matrix) data struct or matrix (size N x dim )
wolffd@0 11 % [inds] From which map units should the local data sets
wolffd@0 12 % be constructed. Interpretation depends on mode
wolffd@0 13 % argument. By default [1:munits].
wolffd@0 14 % 'class': (vector) munits x 1 matrix of class numbers
wolffd@0 15 % 'index': (vector) K x 1 vector of map node indexes
wolffd@0 16 % 'index': (matrix) K x k matrix of map node subscripts
wolffd@0 17 % [mode] (string) 'index' or 'class', if inds is a vector of length
wolffd@0 18 % munits, default is 'class', otherwise 'index'.
wolffd@0 19 % RETURNS
wolffd@0 20 %
wolffd@0 21 % If mode == 'index'
wolffd@0 22 % V (matrix) data vectors hitting the specified nodes (size K x dim)
wolffd@0 23 % I (vector) corresponding data row indexes (size K x 1)
wolffd@0 24 %
wolffd@0 25 % If mode == 'class' (this can be used after using som_select)
wolffd@0 26 % V (cell array) V{K} includes vectors whose BMU has class number
wolffd@0 27 % K in the input matrix 'coord'. Note that
wolffd@0 28 % values of K below 1 are ignored.
wolffd@0 29 % I (cell array) corresponding data indexes in the cell array
wolffd@0 30 %
wolffd@0 31 % NOTE: if the same node is specified multiple times, only one
wolffd@0 32 % set of hits is returned.
wolffd@0 33 %
wolffd@0 34 % See also SOM_BMU, SOM_HITS, SOM_SELECT.
wolffd@0 35
wolffd@0 36 % Version 1.0beta 260997 Johan
wolffd@0 37 % Version 2.0beta 230300 juuso
wolffd@0 38
wolffd@0 39 % Contributed to SOM Toolbox vs2, Mar 23rd, 2000 by Juha Vesanto
wolffd@0 40 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 41
wolffd@0 42 %%%% Init & Check %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 43
wolffd@0 44 error(nargchk(0, 4, nargin)) % check if no. of input args is correct
wolffd@0 45
wolffd@0 46 % map
wolffd@0 47 if isstruct(sMap),
wolffd@0 48 msize = sMap.topol.msize;
wolffd@0 49 dim = size(sMap.codebook,2);
wolffd@0 50 else
wolffd@0 51 msize = [size(sMap,1) 1];
wolffd@0 52 dim = size(sMap,2);
wolffd@0 53 end
wolffd@0 54 munits = prod(msize);
wolffd@0 55
wolffd@0 56 % data
wolffd@0 57 if isstruct(D), D=D.data; end
wolffd@0 58
wolffd@0 59 % inds
wolffd@0 60 if nargin<3, inds = 1:munits; end
wolffd@0 61 isvec = prod(size(inds))==length(inds);
wolffd@0 62
wolffd@0 63 % mode
wolffd@0 64 if nargin<4,
wolffd@0 65 if isvec & length(inds)==munits,
wolffd@0 66 mode = 'class';
wolffd@0 67 else
wolffd@0 68 mode = 'index';
wolffd@0 69 end
wolffd@0 70 end
wolffd@0 71
wolffd@0 72 %%% Action & Build output according to the mode string output
wolffd@0 73
wolffd@0 74 if ~isvec, inds = som_sub2ind(msize,inds); end
wolffd@0 75
wolffd@0 76 bmus=som_bmus(sMap,D);
wolffd@0 77
wolffd@0 78 switch mode
wolffd@0 79 case 'index'
wolffd@0 80 I=find(ismember(bmus,inds));
wolffd@0 81 V=D(I,:);
wolffd@0 82 case 'class'
wolffd@0 83 K=max(inds); % classes
wolffd@0 84 V = cell(K,1);
wolffd@0 85 I = cell(K,1);
wolffd@0 86 for i=1:K,
wolffd@0 87 N_ind=find(inds == i); % indexes of the units of class i
wolffd@0 88 I{i}=find(ismember(bmus,N_ind)); % data indexes
wolffd@0 89 V{i}=D(I{i},:);
wolffd@0 90 end
wolffd@0 91 end
wolffd@0 92
wolffd@0 93
wolffd@0 94
wolffd@0 95
wolffd@0 96