comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_unit_dists.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 Ud = som_unit_dists(topol,lattice,shape)
2
3 %SOM_UNIT_DISTS Distances between unit-locations on the map grid.
4 %
5 % Ud = som_unit_dists(topol,[lattice],[shape])
6 %
7 % Ud = som_unit_dists(sMap);
8 % Ud = som_unit_dists(sMap.topol);
9 % Ud = som_unit_dists(msize, 'hexa', 'cyl');
10 % Ud = som_unit_dists([10 4 4], 'rect', 'toroid');
11 %
12 % Input and output arguments ([]'s are optional):
13 % topol topology of the SOM grid
14 % (struct) topology or map struct
15 % (vector) the 'msize' field of topology struct
16 % [lattice] (string) map lattice, 'rect' by default
17 % [shape] (string) map shape, 'sheet' by default
18 %
19 % Ud (matrix, size [munits munits]) distance from each map unit
20 % to each map unit
21 %
22 % For more help, try 'type som_unit_dists' or check out online documentation.
23 % See also SOM_UNIT_COORDS, SOM_UNIT_NEIGHS.
24
25 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
26 %
27 % som_unit_dists
28 %
29 % PURPOSE
30 %
31 % Returns interunit distances between the units of a Self-Organizing Map
32 % along the map grid.
33 %
34 % SYNTAX
35 %
36 % Ud = som_unit_dists(sTopol);
37 % Ud = som_unit_dists(sM.topol);
38 % Ud = som_unit_dists(msize);
39 % Ud = som_unit_dists(msize,'hexa');
40 % Ud = som_unit_dists(msize,'rect','toroid');
41 %
42 % DESCRIPTION
43 %
44 % Calculates the distances between the units of a SOM based on the
45 % given topology. The distance are euclidian and they are measured
46 % along the map grid (in the output space).
47 %
48 % In case of 'sheet' shape, the distances can be measured directly
49 % from the unit coordinates given by SOM_UNIT_COORDS.
50 %
51 % In case of 'cyl' and 'toroid' shapes this is not so. In these cases
52 % the coordinates are calculated as in the case of 'sheet' shape and
53 % the shape is then taken into account by shifting the map grid into
54 % different positions.
55 %
56 % Consider, for example, a 4x3 map. The basic position of map units
57 % is shown on the left (with '1' - 'C' each denoting one map unit).
58 % In case of a 'cyl' shape, units on the left and right edges are
59 % neighbors, so for this purpose the map is copied on the left and
60 % right sides of the map, as on right.
61 %
62 % basic left basic right
63 % ------- ------- ------- -------
64 % 1 5 9 1 5 9 1 5 9 1 5 9
65 % 2 6 a 2 6 a 2 6 a 2 6 a
66 % 3 7 b 3 7 b 3 7 b 3 7 b
67 % 4 8 c 4 8 c 4 8 c 4 8 c
68 %
69 % For the 'toroid' shape a similar trick is done, except that the
70 % copies are placed all around the basic position:
71 %
72 % 1 5 9 1 5 9 1 5 9
73 % 2 6 a 2 6 a 2 6 a
74 % 3 7 b 3 7 b 3 7 b
75 % 4 8 c 4 8 c 4 8 c
76 % 1 5 9 1 5 9 1 5 9
77 % 2 6 a 2 6 a 2 6 a
78 % 3 7 b 3 7 b 3 7 b
79 % 4 8 c 4 8 c 4 8 c
80 % 1 5 9 1 5 9 1 5 9
81 % 2 6 a 2 6 a 2 6 a
82 % 3 7 b 3 7 b 3 7 b
83 % 4 8 c 4 8 c 4 8 c
84 %
85 % From this we can see that the distance from unit '1' is 1 to units
86 % '9','2','4' and '5', and sqrt(2) to units 'C','A','8' and '6'. Notice
87 % that in the case of a 'hexa' lattice and 'toroid' shape, the size
88 % of the map in y-direction should be even. The reason can be clearly
89 % seen from the two figures below. On the left the basic positions for
90 % a 3x3 map. If the map is copied above itself, it can be seen that the
91 % lattice is broken (on the right):
92 %
93 % basic positions example of broken lattice
94 % --------------- -------------------------
95 % 1 4 7
96 % 2 5 8
97 % 3 6 9
98 % 1 4 7 1 4 7
99 % 2 5 8 2 5 8
100 % 3 6 9 3 6 9
101 %
102 %
103 % REQUIRED INPUT ARGUMENTS
104 %
105 % topol Map grid dimensions.
106 % (struct) topology struct or map struct, the topology
107 % (msize, lattice, shape) of the map is taken from
108 % the appropriate fields (see e.g. SOM_SET)
109 % (vector) the vector which gives the size of the map grid
110 % (msize-field of the topology struct).
111 %
112 % OPTIONAL INPUT ARGUMENTS
113 %
114 % lattice (string) The map lattice, either 'rect' or 'hexa'. Default
115 % is 'rect'. 'hexa' can only be used with 1- or
116 % 2-dimensional map grids.
117 % shape (string) The map shape, either 'sheet', 'cyl' or 'toroid'.
118 % Default is 'sheet'.
119 %
120 % OUTPUT ARGUMENTS
121 %
122 % Ud (matrix) distances from each map unit to each other map unit,
123 % size is [munits munits]
124 %
125 % EXAMPLES
126 %
127 % Simplest case:
128 % Ud = som_unit_dists(sTopol);
129 % Ud = som_unit_dists(sMap.topol);
130 % Ud = som_unit_dists(msize);
131 % Ud = som_unit_dists([10 10]);
132 %
133 % If topology is given as vector, lattice is 'rect' and shape is 'sheet'
134 % by default. To change these, you can use the optional arguments:
135 % Ud = som_unit_dists(msize, 'hexa', 'toroid');
136 %
137 % The distances can also be calculated for high-dimensional grids:
138 % Ud = som_unit_dists([4 4 4 4 4 4]);
139 %
140 % SEE ALSO
141 %
142 % som_unit_coords Calculate grid coordinates.
143 % som_unit_neighs Calculate neighborhoods of map units.
144
145 % Copyright (c) 1997-2000 by the SOM toolbox programming team.
146 % http://www.cis.hut.fi/projects/somtoolbox/
147
148 % Version 1.0beta juuso 110997
149 % Version 2.0beta juuso 101199 170400 070600 130600
150
151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 %% Check arguments
153
154 error(nargchk(1, 3, nargin));
155
156 % default values
157 sTopol = som_set('som_topol','lattice','rect');
158
159 % topol
160 if isstruct(topol),
161 switch topol.type,
162 case 'som_map', sTopol = topol.topol;
163 case 'som_topol', sTopol = topol;
164 end
165 elseif iscell(topol),
166 for i=1:length(topol),
167 if isnumeric(topol{i}), sTopol.msize = topol{i};
168 elseif ischar(topol{i}),
169 switch topol{i},
170 case {'rect','hexa'}, sTopol.lattice = topol{i};
171 case {'sheet','cyl','toroid'}, sTopol.shape = topol{i};
172 end
173 end
174 end
175 else
176 sTopol.msize = topol;
177 end
178 if prod(sTopol.msize)==0, error('Map size is 0.'); end
179
180 % lattice
181 if nargin>1 & ~isempty(lattice) & ~isnan(lattice), sTopol.lattice = lattice; end
182
183 % shape
184 if nargin>2 & ~isempty(shape) & ~isnan(shape), sTopol.shape = shape; end
185
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 %% Action
188
189 msize = sTopol.msize;
190 lattice = sTopol.lattice;
191 shape = sTopol.shape;
192
193 munits = prod(msize);
194 Ud = zeros(munits,munits);
195
196 % free topology
197 if strcmp(lattice,'free'),
198 N1 = sTopol.connection;
199 Ud = som_neighborhood(N1,Inf);
200 end
201
202 % coordinates of map units when the grid is spread on a plane
203 Coords = som_unit_coords(msize,lattice,'sheet');
204
205 % width and height of the grid
206 dx = max(Coords(:,1))-min(Coords(:,1));
207 if msize(1)>1, dx = dx*msize(1)/(msize(1)-1); else dx = dx+1; end
208 dy = max(Coords(:,2))-min(Coords(:,2));
209 if msize(2)>1, dy = dy*msize(2)/(msize(2)-1); else dy = dy+1; end
210
211 % calculate distance from each location to each other location
212 switch shape,
213 case 'sheet',
214 for i=1:(munits-1),
215 inds = [(i+1):munits];
216 Dco = (Coords(inds,:) - Coords(ones(munits-i,1)*i,:))';
217 Ud(i,inds) = sqrt(sum(Dco.^2));
218 end
219
220 case 'cyl',
221 for i=1:(munits-1),
222 inds = [(i+1):munits];
223 Dco = (Coords(inds,:) - Coords(ones(munits-i,1)*i,:))';
224 dist = sum(Dco.^2);
225 % The cylinder shape is taken into account by adding and substracting
226 % the width of the map (dx) from the x-coordinate (ie. shifting the
227 % map right and left).
228 DcoS = Dco; DcoS(1,:) = DcoS(1,:) + dx; %East (x+dx)
229 dist = min(dist,sum(DcoS.^2));
230 DcoS = Dco; DcoS(1,:) = DcoS(1,:) - dx; %West (x-dx)
231 dist = min(dist,sum(DcoS.^2));
232 Ud(i,inds) = sqrt(dist);
233 end
234
235 case 'toroid',
236 for i=1:(munits-1),
237 inds = [(i+1):munits];
238 Dco = (Coords(inds,:) - Coords(ones(munits-i,1)*i,:))';
239 dist = sum(Dco.^2);
240 % The toroid shape is taken into account as the cylinder shape was
241 % (see above), except that the map is shifted also vertically.
242 DcoS = Dco; DcoS(1,:) = DcoS(1,:) + dx; %East (x+dx)
243 dist = min(dist,sum(DcoS.^2));
244 DcoS = Dco; DcoS(1,:) = DcoS(1,:) - dx; %West (x+dx)
245 dist = min(dist,sum(DcoS.^2));
246 DcoS = Dco; DcoS(2,:) = DcoS(2,:) + dy; %South (y+dy)
247 dist = min(dist,sum(DcoS.^2));
248 DcoS = Dco; DcoS(2,:) = DcoS(2,:) - dy; %North (y-dy)
249 dist = min(dist,sum(DcoS.^2));
250 DcoS = Dco; DcoS(1,:) = DcoS(1,:) + dx; DcoS(2,:) = DcoS(2,:) - dy; %NorthEast (x+dx, y-dy)
251 dist = min(dist,sum(DcoS.^2));
252 DcoS = Dco; DcoS(1,:) = DcoS(1,:) + dx; DcoS(2,:) = DcoS(2,:) + dy; %SouthEast (x+dx, y+dy)
253 dist = min(dist,sum(DcoS.^2));
254 DcoS = Dco; DcoS(1,:) = DcoS(1,:) - dx; DcoS(2,:) = DcoS(2,:) + dy; %SouthWest (x-dx, y+dy)
255 dist = min(dist,sum(DcoS.^2));
256 DcoS = Dco; DcoS(1,:) = DcoS(1,:) - dx; DcoS(2,:) = DcoS(2,:) - dy; %NorthWest (x-dx, y-dy)
257 dist = min(dist,sum(DcoS.^2));
258 Ud(i,inds) = sqrt(dist);
259 end
260
261 otherwise,
262 error (['Unknown shape: ', shape]);
263
264 end
265
266 Ud = Ud + Ud';
267
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%