annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_neighbors.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 Ne = som_neighbors(sM,neigh)
wolffd@0 2
wolffd@0 3 % Ne = som_neighbors(sM,neigh)
wolffd@0 4 %
wolffd@0 5 % sM (struct) map or data struct
wolffd@0 6 % (matrix) data matrix, size n x dim
wolffd@0 7 % [neigh] (string) 'kNN' or 'Nk' (which is valid for a SOM only)
wolffd@0 8 % for example '6NN' or 'N1'
wolffd@0 9 % default is '10NN' for a data set and 'N1' for SOM
wolffd@0 10 %
wolffd@0 11 % Ne (matrix) size n x n, a sparse matrix
wolffd@0 12 % indicating the neighbors of each sample by value 1
wolffd@0 13 % (note: the unit itself also has value 0)
wolffd@0 14
wolffd@0 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 16
wolffd@0 17 if isstruct(sM),
wolffd@0 18 switch sM.type,
wolffd@0 19 case 'som_map', M = sM.codebook;
wolffd@0 20 case 'som_data', M = sM.data; sM = [];
wolffd@0 21 end
wolffd@0 22 else
wolffd@0 23 M = sM;
wolffd@0 24 sM = [];
wolffd@0 25 end
wolffd@0 26
wolffd@0 27 n = size(M,1);
wolffd@0 28
wolffd@0 29 if nargin<2,
wolffd@0 30 if isempty(sM), neigh = '10NN'; else neigh = 'N1'; end
wolffd@0 31 end
wolffd@0 32
wolffd@0 33 if strcmp(neigh(end-1:end),'NN'),
wolffd@0 34 k = str2num(neigh(1:end-2));
wolffd@0 35 kmus = som_bmus(M,M,1:k+1);
wolffd@0 36 Ne = sparse(n,n);
wolffd@0 37 for i=1:n, Ne(i,kmus(i,:)) = 1; end
wolffd@0 38 else
wolffd@0 39 if ~isstruct(sM), error('Prototypes must be in a map struct.'); end
wolffd@0 40 k = str2num(neigh(2:end));
wolffd@0 41 N1 = som_unit_neighs(sM);
wolffd@0 42 Ne = sparse(som_neighborhood(N1,k)<=k);
wolffd@0 43 end
wolffd@0 44 Ne([0:n-1]*n+[1:n]) = 0; % remove self from neighbors
wolffd@0 45
wolffd@0 46 return;