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