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