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