wolffd@0
|
1 function H = som_neighf(sMap,radius,neigh,ntype)
|
wolffd@0
|
2
|
wolffd@0
|
3 %SOM_NEIGHF Return neighborhood function values.
|
wolffd@0
|
4 %
|
wolffd@0
|
5 % H = som_neighf(sMap,[radius],[neigh],[ntype]);
|
wolffd@0
|
6 %
|
wolffd@0
|
7 % Input and output arguments ([]'s are optional):
|
wolffd@0
|
8 % sMap (struct) map or topology struct
|
wolffd@0
|
9 % [radius] (scalar) neighborhood radius (by default, the last used value
|
wolffd@0
|
10 % in sMap.trainhist is used, or 1 if that is unavailable)
|
wolffd@0
|
11 % [neigh] (string) neighborhood function type (by default, ..., or
|
wolffd@0
|
12 % 'gaussian' if that is unavailable)
|
wolffd@0
|
13 % [ntype] (string) 'normal' (default), 'probability' or 'mirror'
|
wolffd@0
|
14 %
|
wolffd@0
|
15 % H (matrix) [munits x munits] neighborhood function values from
|
wolffd@0
|
16 % each map unit to each other map unit
|
wolffd@0
|
17 %
|
wolffd@0
|
18 % For more help, try 'type som_batchtrain' or check out online documentation.
|
wolffd@0
|
19 % See also SOM_MAKE, SOM_SEQTRAIN, SOM_TRAIN_STRUCT.
|
wolffd@0
|
20
|
wolffd@0
|
21 % Copyright (c) 1997-2000 by the SOM toolbox programming team.
|
wolffd@0
|
22 % http://www.cis.hut.fi/projects/somtoolbox/
|
wolffd@0
|
23
|
wolffd@0
|
24 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
wolffd@0
|
25 %% Check arguments
|
wolffd@0
|
26
|
wolffd@0
|
27 % defaults
|
wolffd@0
|
28 rdefault = 1;
|
wolffd@0
|
29 ndefault = 'gaussian';
|
wolffd@0
|
30 tdefault = 'normal';
|
wolffd@0
|
31
|
wolffd@0
|
32 % map
|
wolffd@0
|
33 switch sMap.type,
|
wolffd@0
|
34 case 'som_map',
|
wolffd@0
|
35 sTopol = sMap.topol;
|
wolffd@0
|
36 sTrain = sMap.trainhist(end);
|
wolffd@0
|
37 if isempty(sTrain.radius_fin) | isnan(sTrain.radius_fin),
|
wolffd@0
|
38 rdefault = 1;
|
wolffd@0
|
39 else
|
wolffd@0
|
40 rdefault = sTrain.radius_fin;
|
wolffd@0
|
41 end
|
wolffd@0
|
42 if ~isempty(sTrain.neigh) & ~isnan(sTrain.neigh),
|
wolffd@0
|
43 ndefault = sTrain.neigh;
|
wolffd@0
|
44 end
|
wolffd@0
|
45 case 'som_topol', sTopol = sMap;
|
wolffd@0
|
46 end
|
wolffd@0
|
47 munits = prod(sTopol.msize);
|
wolffd@0
|
48
|
wolffd@0
|
49 % other parameters
|
wolffd@0
|
50 if nargin<2 | isempty(radius), radius = rdefault; end
|
wolffd@0
|
51 if nargin<3 | isempty(neigh), neigh = ndefault; end
|
wolffd@0
|
52 if nargin<4 | isempty(ntype), ntype = tdefault; end
|
wolffd@0
|
53
|
wolffd@0
|
54 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
wolffd@0
|
55 %% initialize
|
wolffd@0
|
56
|
wolffd@0
|
57 % basic neighborhood
|
wolffd@0
|
58 Ud = som_unit_dists(sTopol);
|
wolffd@0
|
59 Ud = Ud.^2;
|
wolffd@0
|
60 radius = radius.^2;
|
wolffd@0
|
61 if radius==0, radius = eps; end % zero neighborhood radius may cause div-by-zero error
|
wolffd@0
|
62
|
wolffd@0
|
63 switch ntype,
|
wolffd@0
|
64 case 'normal',
|
wolffd@0
|
65 H = neighf(neigh,Ud,radius);
|
wolffd@0
|
66 case 'probability',
|
wolffd@0
|
67 H = neighf(neigh,Ud,radius);
|
wolffd@0
|
68 for i=1:munits, H(i,:) = H(i,:)/sum(H(i,:)); end
|
wolffd@0
|
69 case 'mirror', % only works for 2-dim grid!!!
|
wolffd@0
|
70 H = zeros(munits,munits);
|
wolffd@0
|
71 Co = som_unit_coords(sTopol);
|
wolffd@0
|
72 for i=-1:1,
|
wolffd@0
|
73 for j=-1:1,
|
wolffd@0
|
74 Ud = gridmirrordist(Co,i,j);
|
wolffd@0
|
75 H = H + neighf(neigh,Ud,radius);
|
wolffd@0
|
76 end
|
wolffd@0
|
77 end
|
wolffd@0
|
78 end
|
wolffd@0
|
79
|
wolffd@0
|
80 return;
|
wolffd@0
|
81
|
wolffd@0
|
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
wolffd@0
|
83 %% subfunctions
|
wolffd@0
|
84
|
wolffd@0
|
85 function H = neighf(neigh,Ud,radius)
|
wolffd@0
|
86
|
wolffd@0
|
87 switch neigh,
|
wolffd@0
|
88 case 'bubble', H = (Ud<=radius);
|
wolffd@0
|
89 case 'gaussian', H = exp(-Ud/(2*radius));
|
wolffd@0
|
90 case 'cutgauss', H = exp(-Ud/(2*radius)) .* (Ud<=radius);
|
wolffd@0
|
91 case 'ep', H = (1-Ud/radius) .* (Ud<=radius);
|
wolffd@0
|
92 end
|
wolffd@0
|
93 return;
|
wolffd@0
|
94
|
wolffd@0
|
95 function Ud = gridmirrordist(Co,mirrorx,mirrory)
|
wolffd@0
|
96
|
wolffd@0
|
97 [munits,mdim] = size(Co);
|
wolffd@0
|
98 if mdim>2, error('Mirrored neighborhood only works for 2-dim map grids.'); end
|
wolffd@0
|
99
|
wolffd@0
|
100 % width and height of the grid
|
wolffd@0
|
101 dx = max(Co(:,1))-min(Co(:,1));
|
wolffd@0
|
102 dy = max(Co(:,2))-min(Co(:,2));
|
wolffd@0
|
103
|
wolffd@0
|
104 % calculate distance from each location to each other location
|
wolffd@0
|
105 Ud = zeros(munits,munits);
|
wolffd@0
|
106 for i=1:munits,
|
wolffd@0
|
107 inds = [i:munits];
|
wolffd@0
|
108 coi = Co(i,:); % take hexagonal shift into account
|
wolffd@0
|
109 coi(1) = coi(1)*(1-2*(mirrorx~=0)) + 2*dx*(mirrorx==1); % +mirrorx * step
|
wolffd@0
|
110 coi(2) = coi(2)*(1-2*(mirrory~=0)) + 2*dy*(mirrory==1); % +mirrory * step
|
wolffd@0
|
111 Dco = (Co(inds,:) - coi(ones(munits-i+1,1),:))';
|
wolffd@0
|
112 Ud(i,inds) = sqrt(sum(Dco.^2));
|
wolffd@0
|
113 Ud(inds,i) = Ud(i,inds)';
|
wolffd@0
|
114 end
|
wolffd@0
|
115 return;
|
wolffd@0
|
116
|
wolffd@0
|
117
|