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