annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_unit_neighs.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 Ne1 = som_unit_neighs(topol,lattice,shape)
wolffd@0 2
wolffd@0 3 %SOM_UNIT_NEIGHS Matrix indicating units in 1-neighborhood for each map unit.
wolffd@0 4 %
wolffd@0 5 % Ne1 = som_unit_neighs(topol,[lattice],[shape])
wolffd@0 6 %
wolffd@0 7 % Ne1 = som_unit_neighs(sTopol);
wolffd@0 8 % Ne1 = som_unit_neighs(sMap.topol);
wolffd@0 9 % Ne1 = som_unit_neighs([10 4], 'hexa', 'cyl');
wolffd@0 10 % Ne1 = som_unit_neighs(msize, 'rect', 'toroid');
wolffd@0 11 %
wolffd@0 12 % Input and output arguments ([]'s are optional):
wolffd@0 13 % topol topology of the SOM grid
wolffd@0 14 % (struct) topology or map struct
wolffd@0 15 % (vector) the 'msize' field of topology struct
wolffd@0 16 % [lattice] (string) map lattice, 'rect' by default
wolffd@0 17 % [shape] (string) map shape, 'sheet' by default
wolffd@0 18 %
wolffd@0 19 % Ne1 (matrix, size [munits munits]) a sparse matrix
wolffd@0 20 % indicating the map units in 1-neighborhood
wolffd@0 21 % by value 1 (note: the unit itself also has value 0)
wolffd@0 22 %
wolffd@0 23 % For more help, try 'type som_unit_neighs' or check out online documentation.
wolffd@0 24 % See also SOM_NEIGHBORHOOD, SOM_UNIT_DISTS, SOM_UNIT_COORDS, SOM_CONNECTION.
wolffd@0 25
wolffd@0 26 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 27 %
wolffd@0 28 % som_unit_neighs
wolffd@0 29 %
wolffd@0 30 % PURPOSE
wolffd@0 31 %
wolffd@0 32 % Find the adjacent (in 1-neighborhood) units for each map unit of a SOM
wolffd@0 33 % based on given topology.
wolffd@0 34 %
wolffd@0 35 % SYNTAX
wolffd@0 36 %
wolffd@0 37 % Ne1 = som_unit_neighs(sMap);
wolffd@0 38 % Ne1 = som_unit_neighs(sM.topol);
wolffd@0 39 % Ne1 = som_unit_neighs(msize);
wolffd@0 40 % Ne1 = som_unit_neighs(msize,'hexa');
wolffd@0 41 % Ne1 = som_unit_neighs(msize,'rect','toroid');
wolffd@0 42 %
wolffd@0 43 % DESCRIPTION
wolffd@0 44 %
wolffd@0 45 % For each map unit, find the units the distance of which from
wolffd@0 46 % the map unit is equal to 1. The distances are calculated
wolffd@0 47 % along the map grid. Consider, for example, the case of a 4x3 map.
wolffd@0 48 % The unit ('1' to 'C') positions for 'rect' and 'hexa' lattice (and
wolffd@0 49 % 'sheet' shape) are depicted below:
wolffd@0 50 %
wolffd@0 51 % 'rect' lattice 'hexa' lattice
wolffd@0 52 % -------------- --------------
wolffd@0 53 % 1 5 9 1 5 9
wolffd@0 54 % 2 6 a 2 6 a
wolffd@0 55 % 3 7 b 3 7 b
wolffd@0 56 % 4 8 c 4 8 c
wolffd@0 57 %
wolffd@0 58 % The units in 1-neighborhood (adjacent units) for unit '6' are '2','5','7'
wolffd@0 59 % and 'a' in the 'rect' case and '5','2','7','9','a' and 'b' in the 'hexa'
wolffd@0 60 % case. The function returns a sparse matrix having value 1 for these units.
wolffd@0 61 % Notice that not all units have equal number of neighbors. Unit '1' has only
wolffd@0 62 % units '2' and '5' in its 1-neighborhood.
wolffd@0 63 %
wolffd@0 64 % REQUIRED INPUT ARGUMENTS
wolffd@0 65 %
wolffd@0 66 % topol Map grid dimensions.
wolffd@0 67 % (struct) topology struct or map struct, the topology
wolffd@0 68 % (msize, lattice, shape) of the map is taken from
wolffd@0 69 % the appropriate fields (see e.g. SOM_SET)
wolffd@0 70 % (vector) the vector which gives the size of the map grid
wolffd@0 71 % (msize-field of the topology struct).
wolffd@0 72 %
wolffd@0 73 % OPTIONAL INPUT ARGUMENTS
wolffd@0 74 %
wolffd@0 75 % lattice (string) The map lattice, either 'rect' or 'hexa'. Default
wolffd@0 76 % is 'rect'. 'hexa' can only be used with 1- or
wolffd@0 77 % 2-dimensional map grids.
wolffd@0 78 % shape (string) The map shape, either 'sheet', 'cyl' or 'toroid'.
wolffd@0 79 % Default is 'sheet'.
wolffd@0 80 %
wolffd@0 81 % OUTPUT ARGUMENTS
wolffd@0 82 %
wolffd@0 83 % Ne1 (matrix) sparse matrix indicating units in 1-neighborhood
wolffd@0 84 % by 1, all others have value 0 (including the unit itself!),
wolffd@0 85 % size is [munits munits]
wolffd@0 86 %
wolffd@0 87 % EXAMPLES
wolffd@0 88 %
wolffd@0 89 % Simplest case:
wolffd@0 90 % Ne1 = som_unit_neighs(sTopol);
wolffd@0 91 % Ne1 = som_unit_neighs(sMap.topol);
wolffd@0 92 % Ne1 = som_unit_neighs(msize);
wolffd@0 93 % Ne1 = som_unit_neighs([10 10]);
wolffd@0 94 %
wolffd@0 95 % If topology is given as vector, lattice is 'rect' and shape is 'sheet'
wolffd@0 96 % by default. To change these, you can use the optional arguments:
wolffd@0 97 % Ne1 = som_unit_neighs(msize, 'hexa', 'toroid');
wolffd@0 98 %
wolffd@0 99 % The neighbors can also be calculated for high-dimensional grids:
wolffd@0 100 % Ne1 = som_unit_neighs([4 4 4 4 4 4]);
wolffd@0 101 %
wolffd@0 102 % SEE ALSO
wolffd@0 103 %
wolffd@0 104 % som_neighborhood Calculate N-neighborhoods of map units.
wolffd@0 105 % som_unit_coords Calculate grid coordinates.
wolffd@0 106 % som_unit_dists Calculate interunit distances.
wolffd@0 107 % som_connection Connection matrix.
wolffd@0 108
wolffd@0 109 % Copyright (c) 1997-2000 by the SOM toolbox programming team.
wolffd@0 110 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 111
wolffd@0 112 % Version 1.0beta juuso 141097
wolffd@0 113 % Version 2.0beta juuso 101199
wolffd@0 114
wolffd@0 115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 116 %% Check arguments
wolffd@0 117
wolffd@0 118 error(nargchk(1, 3, nargin));
wolffd@0 119
wolffd@0 120 % default values
wolffd@0 121 sTopol = som_set('som_topol','lattice','rect');
wolffd@0 122
wolffd@0 123 % topol
wolffd@0 124 if isstruct(topol),
wolffd@0 125 switch topol.type,
wolffd@0 126 case 'som_map', sTopol = topol.topol;
wolffd@0 127 case 'som_topol', sTopol = topol;
wolffd@0 128 end
wolffd@0 129 elseif iscell(topol),
wolffd@0 130 for i=1:length(topol),
wolffd@0 131 if isnumeric(topol{i}), sTopol.msize = topol{i};
wolffd@0 132 elseif ischar(topol{i}),
wolffd@0 133 switch topol{i},
wolffd@0 134 case {'rect','hexa'}, sTopol.lattice = topol{i};
wolffd@0 135 case {'sheet','cyl','toroid'}, sTopol.shape = topol{i};
wolffd@0 136 end
wolffd@0 137 end
wolffd@0 138 end
wolffd@0 139 else
wolffd@0 140 sTopol.msize = topol;
wolffd@0 141 end
wolffd@0 142 if prod(sTopol.msize)==0, error('Map size is 0.'); end
wolffd@0 143
wolffd@0 144 % lattice
wolffd@0 145 if nargin>1 & ~isempty(lattice) & ~isnan(lattice), sTopol.lattice = lattice; end
wolffd@0 146
wolffd@0 147 % shape
wolffd@0 148 if nargin>2 & ~isempty(shape) & ~isnan(shape), sTopol.shape = shape; end
wolffd@0 149
wolffd@0 150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 151 %% Action
wolffd@0 152
wolffd@0 153 % distances between each map unit
wolffd@0 154 Ud = som_unit_dists(sTopol);
wolffd@0 155
wolffd@0 156 % 1-neighborhood are those units the distance of which is equal to 1
wolffd@0 157 munits = prod(sTopol.msize);
wolffd@0 158 Ne1 = sparse(zeros(munits));
wolffd@0 159 for i=1:munits,
wolffd@0 160 inds = find(Ud(i,:)<1.01 & Ud(i,:)>0); % allow for rounding error
wolffd@0 161 Ne1(i,inds) = 1;
wolffd@0 162 end
wolffd@0 163
wolffd@0 164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%