wolffd@0: function Ne = som_neighbors(sM,neigh) wolffd@0: wolffd@0: % Ne = som_neighbors(sM,neigh) wolffd@0: % wolffd@0: % sM (struct) map or data struct wolffd@0: % (matrix) data matrix, size n x dim wolffd@0: % [neigh] (string) 'kNN' or 'Nk' (which is valid for a SOM only) wolffd@0: % for example '6NN' or 'N1' wolffd@0: % default is '10NN' for a data set and 'N1' for SOM wolffd@0: % wolffd@0: % Ne (matrix) size n x n, a sparse matrix wolffd@0: % indicating the neighbors of each sample by value 1 wolffd@0: % (note: the unit itself also has value 0) wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: if isstruct(sM), wolffd@0: switch sM.type, wolffd@0: case 'som_map', M = sM.codebook; wolffd@0: case 'som_data', M = sM.data; sM = []; wolffd@0: end wolffd@0: else wolffd@0: M = sM; wolffd@0: sM = []; wolffd@0: end wolffd@0: wolffd@0: n = size(M,1); wolffd@0: wolffd@0: if nargin<2, wolffd@0: if isempty(sM), neigh = '10NN'; else neigh = 'N1'; end wolffd@0: end wolffd@0: wolffd@0: if strcmp(neigh(end-1:end),'NN'), wolffd@0: k = str2num(neigh(1:end-2)); wolffd@0: kmus = som_bmus(M,M,1:k+1); wolffd@0: Ne = sparse(n,n); wolffd@0: for i=1:n, Ne(i,kmus(i,:)) = 1; end wolffd@0: else wolffd@0: if ~isstruct(sM), error('Prototypes must be in a map struct.'); end wolffd@0: k = str2num(neigh(2:end)); wolffd@0: N1 = som_unit_neighs(sM); wolffd@0: Ne = sparse(som_neighborhood(N1,k)<=k); wolffd@0: end wolffd@0: Ne([0:n-1]*n+[1:n]) = 0; % remove self from neighbors wolffd@0: wolffd@0: return;