wolffd@0: function Ne1 = som_unit_neighs(topol,lattice,shape) wolffd@0: wolffd@0: %SOM_UNIT_NEIGHS Matrix indicating units in 1-neighborhood for each map unit. wolffd@0: % wolffd@0: % Ne1 = som_unit_neighs(topol,[lattice],[shape]) wolffd@0: % wolffd@0: % Ne1 = som_unit_neighs(sTopol); wolffd@0: % Ne1 = som_unit_neighs(sMap.topol); wolffd@0: % Ne1 = som_unit_neighs([10 4], 'hexa', 'cyl'); wolffd@0: % Ne1 = som_unit_neighs(msize, 'rect', 'toroid'); wolffd@0: % wolffd@0: % Input and output arguments ([]'s are optional): wolffd@0: % topol topology of the SOM grid wolffd@0: % (struct) topology or map struct wolffd@0: % (vector) the 'msize' field of topology struct wolffd@0: % [lattice] (string) map lattice, 'rect' by default wolffd@0: % [shape] (string) map shape, 'sheet' by default wolffd@0: % wolffd@0: % Ne1 (matrix, size [munits munits]) a sparse matrix wolffd@0: % indicating the map units in 1-neighborhood wolffd@0: % by value 1 (note: the unit itself also has value 0) wolffd@0: % wolffd@0: % For more help, try 'type som_unit_neighs' or check out online documentation. wolffd@0: % See also SOM_NEIGHBORHOOD, SOM_UNIT_DISTS, SOM_UNIT_COORDS, SOM_CONNECTION. wolffd@0: wolffd@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % wolffd@0: % som_unit_neighs wolffd@0: % wolffd@0: % PURPOSE wolffd@0: % wolffd@0: % Find the adjacent (in 1-neighborhood) units for each map unit of a SOM wolffd@0: % based on given topology. wolffd@0: % wolffd@0: % SYNTAX wolffd@0: % wolffd@0: % Ne1 = som_unit_neighs(sMap); wolffd@0: % Ne1 = som_unit_neighs(sM.topol); wolffd@0: % Ne1 = som_unit_neighs(msize); wolffd@0: % Ne1 = som_unit_neighs(msize,'hexa'); wolffd@0: % Ne1 = som_unit_neighs(msize,'rect','toroid'); wolffd@0: % wolffd@0: % DESCRIPTION wolffd@0: % wolffd@0: % For each map unit, find the units the distance of which from wolffd@0: % the map unit is equal to 1. The distances are calculated wolffd@0: % along the map grid. Consider, for example, the case of a 4x3 map. wolffd@0: % The unit ('1' to 'C') positions for 'rect' and 'hexa' lattice (and wolffd@0: % 'sheet' shape) are depicted below: wolffd@0: % wolffd@0: % 'rect' lattice 'hexa' lattice wolffd@0: % -------------- -------------- wolffd@0: % 1 5 9 1 5 9 wolffd@0: % 2 6 a 2 6 a wolffd@0: % 3 7 b 3 7 b wolffd@0: % 4 8 c 4 8 c wolffd@0: % wolffd@0: % The units in 1-neighborhood (adjacent units) for unit '6' are '2','5','7' wolffd@0: % and 'a' in the 'rect' case and '5','2','7','9','a' and 'b' in the 'hexa' wolffd@0: % case. The function returns a sparse matrix having value 1 for these units. wolffd@0: % Notice that not all units have equal number of neighbors. Unit '1' has only wolffd@0: % units '2' and '5' in its 1-neighborhood. wolffd@0: % wolffd@0: % REQUIRED INPUT ARGUMENTS wolffd@0: % wolffd@0: % topol Map grid dimensions. wolffd@0: % (struct) topology struct or map struct, the topology wolffd@0: % (msize, lattice, shape) of the map is taken from wolffd@0: % the appropriate fields (see e.g. SOM_SET) wolffd@0: % (vector) the vector which gives the size of the map grid wolffd@0: % (msize-field of the topology struct). wolffd@0: % wolffd@0: % OPTIONAL INPUT ARGUMENTS wolffd@0: % wolffd@0: % lattice (string) The map lattice, either 'rect' or 'hexa'. Default wolffd@0: % is 'rect'. 'hexa' can only be used with 1- or wolffd@0: % 2-dimensional map grids. wolffd@0: % shape (string) The map shape, either 'sheet', 'cyl' or 'toroid'. wolffd@0: % Default is 'sheet'. wolffd@0: % wolffd@0: % OUTPUT ARGUMENTS wolffd@0: % wolffd@0: % Ne1 (matrix) sparse matrix indicating units in 1-neighborhood wolffd@0: % by 1, all others have value 0 (including the unit itself!), wolffd@0: % size is [munits munits] wolffd@0: % wolffd@0: % EXAMPLES wolffd@0: % wolffd@0: % Simplest case: wolffd@0: % Ne1 = som_unit_neighs(sTopol); wolffd@0: % Ne1 = som_unit_neighs(sMap.topol); wolffd@0: % Ne1 = som_unit_neighs(msize); wolffd@0: % Ne1 = som_unit_neighs([10 10]); wolffd@0: % wolffd@0: % If topology is given as vector, lattice is 'rect' and shape is 'sheet' wolffd@0: % by default. To change these, you can use the optional arguments: wolffd@0: % Ne1 = som_unit_neighs(msize, 'hexa', 'toroid'); wolffd@0: % wolffd@0: % The neighbors can also be calculated for high-dimensional grids: wolffd@0: % Ne1 = som_unit_neighs([4 4 4 4 4 4]); wolffd@0: % wolffd@0: % SEE ALSO wolffd@0: % wolffd@0: % som_neighborhood Calculate N-neighborhoods of map units. wolffd@0: % som_unit_coords Calculate grid coordinates. wolffd@0: % som_unit_dists Calculate interunit distances. wolffd@0: % som_connection Connection matrix. wolffd@0: wolffd@0: % Copyright (c) 1997-2000 by the SOM toolbox programming team. wolffd@0: % http://www.cis.hut.fi/projects/somtoolbox/ wolffd@0: wolffd@0: % Version 1.0beta juuso 141097 wolffd@0: % Version 2.0beta juuso 101199 wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% Check arguments wolffd@0: wolffd@0: error(nargchk(1, 3, nargin)); wolffd@0: wolffd@0: % default values wolffd@0: sTopol = som_set('som_topol','lattice','rect'); wolffd@0: wolffd@0: % topol wolffd@0: if isstruct(topol), wolffd@0: switch topol.type, wolffd@0: case 'som_map', sTopol = topol.topol; wolffd@0: case 'som_topol', sTopol = topol; wolffd@0: end wolffd@0: elseif iscell(topol), wolffd@0: for i=1:length(topol), wolffd@0: if isnumeric(topol{i}), sTopol.msize = topol{i}; wolffd@0: elseif ischar(topol{i}), wolffd@0: switch topol{i}, wolffd@0: case {'rect','hexa'}, sTopol.lattice = topol{i}; wolffd@0: case {'sheet','cyl','toroid'}, sTopol.shape = topol{i}; wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: else wolffd@0: sTopol.msize = topol; wolffd@0: end wolffd@0: if prod(sTopol.msize)==0, error('Map size is 0.'); end wolffd@0: wolffd@0: % lattice wolffd@0: if nargin>1 & ~isempty(lattice) & ~isnan(lattice), sTopol.lattice = lattice; end wolffd@0: wolffd@0: % shape wolffd@0: if nargin>2 & ~isempty(shape) & ~isnan(shape), sTopol.shape = shape; end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% Action wolffd@0: wolffd@0: % distances between each map unit wolffd@0: Ud = som_unit_dists(sTopol); wolffd@0: wolffd@0: % 1-neighborhood are those units the distance of which is equal to 1 wolffd@0: munits = prod(sTopol.msize); wolffd@0: Ne1 = sparse(zeros(munits)); wolffd@0: for i=1:munits, wolffd@0: inds = find(Ud(i,:)<1.01 & Ud(i,:)>0); % allow for rounding error wolffd@0: Ne1(i,inds) = 1; wolffd@0: end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%