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

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