comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_neighborhood.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_neighborhood(Ne1,n)
2
3 %SOM_NEIGHBORHOOD Calculate neighborhood matrix.
4 %
5 % Ne = som_neighborhood(Ne1,n)
6 %
7 % Ne = som_neighborhood(Ne1);
8 % Ne = som_neighborhood(som_unit_neighs(topol),2);
9 %
10 % Input and output arguments ([]'s are optional):
11 % Ne1 (matrix, size [munits m]) a sparse matrix indicating
12 % the units in 1-neighborhood for each map unit
13 % [n] (scalar) maximum neighborhood which is calculated, default=Inf
14 %
15 % Ne (matrix, size [munits munits]) neighborhood matrix,
16 % each row (and column) contains neighborhood
17 % values from the specific map unit to all other
18 % map units, or Inf if the value is unknown.
19 %
20 % For more help, try 'type som_neighborhood' or check out online documentation.
21 % See also SOM_UNIT_NEIGHS, SOM_UNIT_DISTS, SOM_UNIT_COORDS, SOM_CONNECTION.
22
23 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
24 %
25 % som_neighborhood
26 %
27 % PURPOSE
28 %
29 % Calculate to which neighborhood each map unit belongs to relative to
30 % each other map unit, given the units in 1-neighborhood of each unit.
31 %
32 % SYNTAX
33 %
34 % Ne = som_neighborhood(Ne1);
35 % Ne = som_neighborhood(Ne1,n);
36 %
37 % DESCRIPTION
38 %
39 % For each map unit, finds the minimum neighborhood to which it belongs
40 % to relative to each other map unit. Or, equivalently, for each map
41 % unit, finds which units form its k-neighborhood, where k goes from
42 % 0 to n.
43 %
44 % The neighborhood is calculated iteratively using the reflexivity of
45 % neighborhood.
46 % let N1i be the 1-neighborhood set a unit i
47 % and let N11i be the set of units in the 1-neighborhood of any unit j in N1i
48 % then N2i (the 2-neighborhood set of unit i) is N11i \ N1i
49 %
50 % Consider, for example, the case of a 5x5 map. The neighborhood in case of
51 % 'rect' and 'hexa' lattices (and 'sheet' shape) for the unit at the
52 % center of the map are depicted below:
53 %
54 % 'rect' lattice 'hexa' lattice
55 % -------------- --------------
56 % 4 3 2 3 4 3 2 2 2 3
57 % 3 2 1 2 3 2 1 1 2 3
58 % 2 1 0 1 2 2 1 0 1 2
59 % 3 2 1 2 3 2 1 1 2 3
60 % 4 3 2 3 4 3 2 2 2 3
61 %
62 % Because the iterative procedure is rather slow, the neighborhoods
63 % are calculated upto given maximal value. The uncalculated values
64 % in the returned matrix are Inf:s.
65 %
66 % REQUIRED INPUT ARGUMENTS
67 %
68 % Ne1 (matrix) Each row contains 1, if the corresponding unit is adjacent
69 % for that map unit, 0 otherwise. This can be calculated
70 % using SOM_UNIT_NEIGHS. The matrix can be sparse.
71 % Size munits x munits.
72 %
73 % OPTIONAL INPUT ARGUMENTS
74 %
75 % n (scalar) Maximal neighborhood value which is calculated,
76 % Inf by default (all neighborhoods).
77 %
78 % OUTPUT ARGUMENTS
79 %
80 % Ne (matrix) neighborhood values for each map unit, size is
81 % [munits, munits]. The matrix contains the minimum
82 % neighborhood of unit i, to which unit j belongs,
83 % or Inf, if the neighborhood was bigger than n.
84 %
85 % EXAMPLES
86 %
87 % Ne = som_neighborhood(Ne1,1); % upto 1-neighborhood
88 % Ne = som_neighborhood(Ne1,Inf); % all neighborhoods
89 % Ne = som_neighborhood(som_unit_neighs(topol),4);
90 %
91 % SEE ALSO
92 %
93 % som_unit_neighs Calculate units in 1-neighborhood for each map unit.
94 % som_unit_coords Calculate grid coordinates.
95 % som_unit_dists Calculate interunit distances.
96 % som_connection Connection matrix.
97
98 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
99 % http://www.cis.hut.fi/projects/somtoolbox/
100
101 % Version 1.0beta juuso 141097
102 % Version 2.0beta juuso 101199
103
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 %% Check arguments
106
107 error(nargchk(1, 2, nargin));
108
109 if nargin<2, n=Inf; end
110
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 %% Action
113
114 % initialize
115 if issparse(Ne1), Ne = full(Ne1); else Ne = Ne1; end
116 clear Ne1
117 [munits dummy] = size(Ne);
118 Ne(find(Ne==0)) = NaN;
119 for i=1:munits, Ne(i,i)=0; end
120
121 % Calculate neighborhood distance for each unit using reflexsivity
122 % of neighborhood:
123 % let N1i be the 1-neighborhood set a unit i
124 % then N2i is the union of all map units, belonging to the
125 % 1-neighborhood of any unit j in N1i, not already in N1i
126 k=1;
127 if n>1,
128 fprintf(1,'Calculating neighborhood: 1 ');
129 N1 = Ne;
130 N1(find(N1~=1)) = 0;
131 end
132 while k<n & any(isnan(Ne(:))),
133 k=k+1;
134 fprintf(1,'%d ',k);
135 for i=1:munits,
136 candidates = isnan(Ne(i,:)); % units not in any neighborhood yet
137 if any(candidates),
138 prevneigh = find(Ne(i,:)==k-1); % neighborhood (k-1)
139 N1_of_prevneigh = any(N1(prevneigh,:)); % union of their N1:s
140 Nn = find(N1_of_prevneigh & candidates);
141 if length(Nn), Ne(i,Nn) = k; Ne(Nn,i) = k; end
142 end
143 end
144 end
145 if n>1, fprintf(1,'\n'); end
146
147 % finally replace all uncalculated distance values with Inf
148 Ne(find(isnan(Ne))) = Inf;
149
150 return;
151
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 %% faster version?
154
155 l = size(Ne1,1); Ne1([0:l-1]*(l+1)+1) = 1; Ne = full(Ne1); M0 = Ne1; k = 2;
156 while any(Ne(:)==0), M1=(M0*Ne1>0); Ne(find(M1-M0))=k; M0=M1; k=k+1; end
157 Ne([0:l-1]*(l+1)+1) = 0;