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