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