comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/som_connection.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 C=som_connection(S)
2
3 %SOM_CONNECTION Connection matrix for 'hexa' and 'rect' lattices
4 %
5 % C=som_connection(S)
6 %
7 % C=som_connection(sMap);
8 % C=som_connection(sTopol);
9 % C=som_connection({'hexa', [6 5], 'sheet'});
10 %
11 % Input and output arguments:
12 % S (struct) map or topol struct
13 % (cell array) a cell array of form {lattice, msize, shape}, where
14 % lattice: 'hexa' or 'rect'
15 % msize : 1x2 vector
16 % shape : 'sheet', 'cyl or 'toroid'
17 %
18 % C (sparse) An NxN connection matrix, N=prod(msize)
19 %
20 % The function returns a connection matrix, e.g., for drawing
21 % connections between map units in the function som_grid. Note that
22 % the connections are defined only in the upper triangular part to
23 % save some memory!! Function SOM_UNIT_NEIGHS does the same thing,
24 % but also has values in the lower triangular. It is also slower.
25 %
26 % For more help, try 'type som_connection' or check out online documentation.
27 % See also SOM_GRID, SOM_UNIT_NEIGHS.
28
29 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 %
31 % som_connection
32 %
33 % PURPOSE
34 %
35 % To create a connection matrix of SOM 'hexa' and 'rect' negihborhoods
36 %
37 % SYNTAX
38 %
39 % C = som_connection(S)
40 %
41 % DESCRIPTION
42 %
43 % Creates a connection matrix of SOM 'hexa' and 'rect'
44 % neighborhoods. The connections are defined only in the upper
45 % triangular part to save some memory.
46 %
47 % Function SOM_UNIT_NEIGHS does the same thing, but also has values
48 % in the lower triangular. It is also slower, except for
49 % 'toroid' shape because in that case this function calls
50 % SOM_UNIT_NEIGHS...
51 %
52 % REQUIRED INPUT ARGUMENTS
53 %
54 % S map topology
55 % (map struct) S.topol is used to build the matrix
56 % (topol struct) topology information is used to build the matrix
57 % (cell array) of form {lattice, msize, shape}, where
58 % lattice: 'hexa' or 'rect'
59 % msize : 1x2 vector
60 % shape : 'sheet', 'cyl or 'toroid'
61 %
62 % OUTPUT ARGUMENTS
63 %
64 % C (sparse) munits x munits sparse matrix which describes
65 % nearest neighbor connections between units
66 %
67 % EXAMPLE
68 %
69 % C = som_connection('hexa',[3 4],'sheet');
70 % full(C)
71 % ans =
72 %
73 % 0 1 0 1 0 0 0 0 0 0 0 0
74 % 0 0 1 1 1 1 0 0 0 0 0 0
75 % 0 0 0 0 0 1 0 0 0 0 0 0
76 % 0 0 0 0 1 0 1 0 0 0 0 0
77 % 0 0 0 0 0 1 1 1 1 0 0 0
78 % 0 0 0 0 0 0 0 0 1 0 0 0
79 % 0 0 0 0 0 0 0 1 0 1 0 0
80 % 0 0 0 0 0 0 0 0 1 1 1 1
81 % 0 0 0 0 0 0 0 0 0 0 0 1
82 % 0 0 0 0 0 0 0 0 0 0 1 0
83 % 0 0 0 0 0 0 0 0 0 0 0 1
84 % 0 0 0 0 0 0 0 0 0 0 0 0
85 %
86 % SEE ALSO
87 %
88 % som_grid Visualization of a SOM grid
89 % som_unit_neighs Units in 1-neighborhood for all map units.
90
91 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
92 % http://www.cis.hut.fi/projects/somtoolbox/
93
94 % Version 2.0alpha Johan 061099 juuso 151199 170400
95
96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 % Check arguments
98
99 error(nargchk(1, 1, nargin)); % check number of input arguments
100
101 [tmp,ok,tmp]=som_set(S);
102 if isstruct(S) & all(ok), % check m type
103 switch S.type
104 case 'som_topol'
105 msize=S.msize;
106 lattice=S.lattice;
107 shape=S.shape;
108 case 'som_map'
109 msize=S.topol.msize;
110 lattice=S.topol.lattice;
111 shape=S.topol.shape;
112 otherwise
113 error('Invalid map or topol struct.');
114 end
115 elseif iscell(S),
116 if vis_valuetype(S,{'topol_cell'}),
117 lattice=S{1};
118 msize=S{2};
119 shape=S{3};
120 else
121 error('{lattice, msize, shape} expected for cell input.')
122 end
123 else
124 error('{lattice, msize, shape}, or map or topol struct expected.')
125 end
126
127 if ~vis_valuetype(msize,{'1x2'})
128 error('Invalid map size: only 2D maps allowed.')
129 end
130
131 %% Init %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132
133 N=msize(1)*msize(2);
134
135 %% Action & Build output arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
136
137 switch lattice
138 case 'hexa'
139 l1=ones(N,1); l1((msize(1)+1):msize(1):end)=0;
140 l2=zeros(msize(1),1); l3=l2;
141 l2(1:2:end-1)=1; l3(3:2:end)=1;
142 l2=repmat(l2,msize(2),1);
143 l3=repmat(l3,msize(2),1);
144 C= ...
145 spdiags([l1 l2 ones(N,1) l3], [1 msize(1)-1:msize(1)+1],N,N);
146 case 'rect'
147 l1=ones(N,1);l1((msize(1)+1):msize(1):end)=0;
148 C=spdiags([l1 ones(N,1)],[1 msize(1)],N,N);
149 otherwise
150 error('Unknown lattice.')
151 end
152
153 switch shape
154 case 'sheet'
155 ;
156 case 'cyl'
157 C=spdiags(ones(N,1),msize(1)*(msize(2)-1),C);
158 case 'toroid'
159 %warning('Toroid not yet implemented: using ''cyl''.');
160 %C=spdiags(ones(N,1),msize(1)*(msize(2)-1),C);
161 %l=zeros(N,1); l(1:msize(2):end)=1;
162 %C=spdiags(l,msize(1),C);
163
164 % use som_unit_neighs to calculate these
165 C = som_unit_neighs(msize,lattice,'toroid');
166 % to be consistent, set the lower triangular values to zero
167 munits = prod(msize);
168 for i=1:(munits-1), C((i+1):munits,i) = 0; end
169 otherwise
170 error('Unknown shape.');
171 end
172
173