annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_connection.m @ 0:cc4b1211e677 tip

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