annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_neighborhood.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 Ne = som_neighborhood(Ne1,n)
wolffd@0 2
wolffd@0 3 %SOM_NEIGHBORHOOD Calculate neighborhood matrix.
wolffd@0 4 %
wolffd@0 5 % Ne = som_neighborhood(Ne1,n)
wolffd@0 6 %
wolffd@0 7 % Ne = som_neighborhood(Ne1);
wolffd@0 8 % Ne = som_neighborhood(som_unit_neighs(topol),2);
wolffd@0 9 %
wolffd@0 10 % Input and output arguments ([]'s are optional):
wolffd@0 11 % Ne1 (matrix, size [munits m]) a sparse matrix indicating
wolffd@0 12 % the units in 1-neighborhood for each map unit
wolffd@0 13 % [n] (scalar) maximum neighborhood which is calculated, default=Inf
wolffd@0 14 %
wolffd@0 15 % Ne (matrix, size [munits munits]) neighborhood matrix,
wolffd@0 16 % each row (and column) contains neighborhood
wolffd@0 17 % values from the specific map unit to all other
wolffd@0 18 % map units, or Inf if the value is unknown.
wolffd@0 19 %
wolffd@0 20 % For more help, try 'type som_neighborhood' or check out online documentation.
wolffd@0 21 % See also SOM_UNIT_NEIGHS, SOM_UNIT_DISTS, SOM_UNIT_COORDS, SOM_CONNECTION.
wolffd@0 22
wolffd@0 23 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 24 %
wolffd@0 25 % som_neighborhood
wolffd@0 26 %
wolffd@0 27 % PURPOSE
wolffd@0 28 %
wolffd@0 29 % Calculate to which neighborhood each map unit belongs to relative to
wolffd@0 30 % each other map unit, given the units in 1-neighborhood of each unit.
wolffd@0 31 %
wolffd@0 32 % SYNTAX
wolffd@0 33 %
wolffd@0 34 % Ne = som_neighborhood(Ne1);
wolffd@0 35 % Ne = som_neighborhood(Ne1,n);
wolffd@0 36 %
wolffd@0 37 % DESCRIPTION
wolffd@0 38 %
wolffd@0 39 % For each map unit, finds the minimum neighborhood to which it belongs
wolffd@0 40 % to relative to each other map unit. Or, equivalently, for each map
wolffd@0 41 % unit, finds which units form its k-neighborhood, where k goes from
wolffd@0 42 % 0 to n.
wolffd@0 43 %
wolffd@0 44 % The neighborhood is calculated iteratively using the reflexivity of
wolffd@0 45 % neighborhood.
wolffd@0 46 % let N1i be the 1-neighborhood set a unit i
wolffd@0 47 % and let N11i be the set of units in the 1-neighborhood of any unit j in N1i
wolffd@0 48 % then N2i (the 2-neighborhood set of unit i) is N11i \ N1i
wolffd@0 49 %
wolffd@0 50 % Consider, for example, the case of a 5x5 map. The neighborhood in case of
wolffd@0 51 % 'rect' and 'hexa' lattices (and 'sheet' shape) for the unit at the
wolffd@0 52 % center of the map are depicted below:
wolffd@0 53 %
wolffd@0 54 % 'rect' lattice 'hexa' lattice
wolffd@0 55 % -------------- --------------
wolffd@0 56 % 4 3 2 3 4 3 2 2 2 3
wolffd@0 57 % 3 2 1 2 3 2 1 1 2 3
wolffd@0 58 % 2 1 0 1 2 2 1 0 1 2
wolffd@0 59 % 3 2 1 2 3 2 1 1 2 3
wolffd@0 60 % 4 3 2 3 4 3 2 2 2 3
wolffd@0 61 %
wolffd@0 62 % Because the iterative procedure is rather slow, the neighborhoods
wolffd@0 63 % are calculated upto given maximal value. The uncalculated values
wolffd@0 64 % in the returned matrix are Inf:s.
wolffd@0 65 %
wolffd@0 66 % REQUIRED INPUT ARGUMENTS
wolffd@0 67 %
wolffd@0 68 % Ne1 (matrix) Each row contains 1, if the corresponding unit is adjacent
wolffd@0 69 % for that map unit, 0 otherwise. This can be calculated
wolffd@0 70 % using SOM_UNIT_NEIGHS. The matrix can be sparse.
wolffd@0 71 % Size munits x munits.
wolffd@0 72 %
wolffd@0 73 % OPTIONAL INPUT ARGUMENTS
wolffd@0 74 %
wolffd@0 75 % n (scalar) Maximal neighborhood value which is calculated,
wolffd@0 76 % Inf by default (all neighborhoods).
wolffd@0 77 %
wolffd@0 78 % OUTPUT ARGUMENTS
wolffd@0 79 %
wolffd@0 80 % Ne (matrix) neighborhood values for each map unit, size is
wolffd@0 81 % [munits, munits]. The matrix contains the minimum
wolffd@0 82 % neighborhood of unit i, to which unit j belongs,
wolffd@0 83 % or Inf, if the neighborhood was bigger than n.
wolffd@0 84 %
wolffd@0 85 % EXAMPLES
wolffd@0 86 %
wolffd@0 87 % Ne = som_neighborhood(Ne1,1); % upto 1-neighborhood
wolffd@0 88 % Ne = som_neighborhood(Ne1,Inf); % all neighborhoods
wolffd@0 89 % Ne = som_neighborhood(som_unit_neighs(topol),4);
wolffd@0 90 %
wolffd@0 91 % SEE ALSO
wolffd@0 92 %
wolffd@0 93 % som_unit_neighs Calculate units in 1-neighborhood for each map unit.
wolffd@0 94 % som_unit_coords Calculate grid coordinates.
wolffd@0 95 % som_unit_dists Calculate interunit distances.
wolffd@0 96 % som_connection Connection matrix.
wolffd@0 97
wolffd@0 98 % Copyright (c) 1999-2000 by the SOM toolbox programming team.
wolffd@0 99 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 100
wolffd@0 101 % Version 1.0beta juuso 141097
wolffd@0 102 % Version 2.0beta juuso 101199
wolffd@0 103
wolffd@0 104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 105 %% Check arguments
wolffd@0 106
wolffd@0 107 error(nargchk(1, 2, nargin));
wolffd@0 108
wolffd@0 109 if nargin<2, n=Inf; end
wolffd@0 110
wolffd@0 111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 112 %% Action
wolffd@0 113
wolffd@0 114 % initialize
wolffd@0 115 if issparse(Ne1), Ne = full(Ne1); else Ne = Ne1; end
wolffd@0 116 clear Ne1
wolffd@0 117 [munits dummy] = size(Ne);
wolffd@0 118 Ne(find(Ne==0)) = NaN;
wolffd@0 119 for i=1:munits, Ne(i,i)=0; end
wolffd@0 120
wolffd@0 121 % Calculate neighborhood distance for each unit using reflexsivity
wolffd@0 122 % of neighborhood:
wolffd@0 123 % let N1i be the 1-neighborhood set a unit i
wolffd@0 124 % then N2i is the union of all map units, belonging to the
wolffd@0 125 % 1-neighborhood of any unit j in N1i, not already in N1i
wolffd@0 126 k=1;
wolffd@0 127 if n>1,
wolffd@0 128 fprintf(1,'Calculating neighborhood: 1 ');
wolffd@0 129 N1 = Ne;
wolffd@0 130 N1(find(N1~=1)) = 0;
wolffd@0 131 end
wolffd@0 132 while k<n & any(isnan(Ne(:))),
wolffd@0 133 k=k+1;
wolffd@0 134 fprintf(1,'%d ',k);
wolffd@0 135 for i=1:munits,
wolffd@0 136 candidates = isnan(Ne(i,:)); % units not in any neighborhood yet
wolffd@0 137 if any(candidates),
wolffd@0 138 prevneigh = find(Ne(i,:)==k-1); % neighborhood (k-1)
wolffd@0 139 N1_of_prevneigh = any(N1(prevneigh,:)); % union of their N1:s
wolffd@0 140 Nn = find(N1_of_prevneigh & candidates);
wolffd@0 141 if length(Nn), Ne(i,Nn) = k; Ne(Nn,i) = k; end
wolffd@0 142 end
wolffd@0 143 end
wolffd@0 144 end
wolffd@0 145 if n>1, fprintf(1,'\n'); end
wolffd@0 146
wolffd@0 147 % finally replace all uncalculated distance values with Inf
wolffd@0 148 Ne(find(isnan(Ne))) = Inf;
wolffd@0 149
wolffd@0 150 return;
wolffd@0 151
wolffd@0 152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 153 %% faster version?
wolffd@0 154
wolffd@0 155 l = size(Ne1,1); Ne1([0:l-1]*(l+1)+1) = 1; Ne = full(Ne1); M0 = Ne1; k = 2;
wolffd@0 156 while any(Ne(:)==0), M1=(M0*Ne1>0); Ne(find(M1-M0))=k; M0=M1; k=k+1; end
wolffd@0 157 Ne([0:l-1]*(l+1)+1) = 0;