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