wolffd@0: function Ne = som_neighborhood(Ne1,n) wolffd@0: wolffd@0: %SOM_NEIGHBORHOOD Calculate neighborhood matrix. wolffd@0: % wolffd@0: % Ne = som_neighborhood(Ne1,n) wolffd@0: % wolffd@0: % Ne = som_neighborhood(Ne1); wolffd@0: % Ne = som_neighborhood(som_unit_neighs(topol),2); wolffd@0: % wolffd@0: % Input and output arguments ([]'s are optional): wolffd@0: % Ne1 (matrix, size [munits m]) a sparse matrix indicating wolffd@0: % the units in 1-neighborhood for each map unit wolffd@0: % [n] (scalar) maximum neighborhood which is calculated, default=Inf wolffd@0: % wolffd@0: % Ne (matrix, size [munits munits]) neighborhood matrix, wolffd@0: % each row (and column) contains neighborhood wolffd@0: % values from the specific map unit to all other wolffd@0: % map units, or Inf if the value is unknown. wolffd@0: % wolffd@0: % For more help, try 'type som_neighborhood' or check out online documentation. wolffd@0: % See also SOM_UNIT_NEIGHS, SOM_UNIT_DISTS, SOM_UNIT_COORDS, SOM_CONNECTION. wolffd@0: wolffd@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % wolffd@0: % som_neighborhood wolffd@0: % wolffd@0: % PURPOSE wolffd@0: % wolffd@0: % Calculate to which neighborhood each map unit belongs to relative to wolffd@0: % each other map unit, given the units in 1-neighborhood of each unit. wolffd@0: % wolffd@0: % SYNTAX wolffd@0: % wolffd@0: % Ne = som_neighborhood(Ne1); wolffd@0: % Ne = som_neighborhood(Ne1,n); wolffd@0: % wolffd@0: % DESCRIPTION wolffd@0: % wolffd@0: % For each map unit, finds the minimum neighborhood to which it belongs wolffd@0: % to relative to each other map unit. Or, equivalently, for each map wolffd@0: % unit, finds which units form its k-neighborhood, where k goes from wolffd@0: % 0 to n. wolffd@0: % wolffd@0: % The neighborhood is calculated iteratively using the reflexivity of wolffd@0: % neighborhood. wolffd@0: % let N1i be the 1-neighborhood set a unit i wolffd@0: % and let N11i be the set of units in the 1-neighborhood of any unit j in N1i wolffd@0: % then N2i (the 2-neighborhood set of unit i) is N11i \ N1i wolffd@0: % wolffd@0: % Consider, for example, the case of a 5x5 map. The neighborhood in case of wolffd@0: % 'rect' and 'hexa' lattices (and 'sheet' shape) for the unit at the wolffd@0: % center of the map are depicted below: wolffd@0: % wolffd@0: % 'rect' lattice 'hexa' lattice wolffd@0: % -------------- -------------- wolffd@0: % 4 3 2 3 4 3 2 2 2 3 wolffd@0: % 3 2 1 2 3 2 1 1 2 3 wolffd@0: % 2 1 0 1 2 2 1 0 1 2 wolffd@0: % 3 2 1 2 3 2 1 1 2 3 wolffd@0: % 4 3 2 3 4 3 2 2 2 3 wolffd@0: % wolffd@0: % Because the iterative procedure is rather slow, the neighborhoods wolffd@0: % are calculated upto given maximal value. The uncalculated values wolffd@0: % in the returned matrix are Inf:s. wolffd@0: % wolffd@0: % REQUIRED INPUT ARGUMENTS wolffd@0: % wolffd@0: % Ne1 (matrix) Each row contains 1, if the corresponding unit is adjacent wolffd@0: % for that map unit, 0 otherwise. This can be calculated wolffd@0: % using SOM_UNIT_NEIGHS. The matrix can be sparse. wolffd@0: % Size munits x munits. wolffd@0: % wolffd@0: % OPTIONAL INPUT ARGUMENTS wolffd@0: % wolffd@0: % n (scalar) Maximal neighborhood value which is calculated, wolffd@0: % Inf by default (all neighborhoods). wolffd@0: % wolffd@0: % OUTPUT ARGUMENTS wolffd@0: % wolffd@0: % Ne (matrix) neighborhood values for each map unit, size is wolffd@0: % [munits, munits]. The matrix contains the minimum wolffd@0: % neighborhood of unit i, to which unit j belongs, wolffd@0: % or Inf, if the neighborhood was bigger than n. wolffd@0: % wolffd@0: % EXAMPLES wolffd@0: % wolffd@0: % Ne = som_neighborhood(Ne1,1); % upto 1-neighborhood wolffd@0: % Ne = som_neighborhood(Ne1,Inf); % all neighborhoods wolffd@0: % Ne = som_neighborhood(som_unit_neighs(topol),4); wolffd@0: % wolffd@0: % SEE ALSO wolffd@0: % wolffd@0: % som_unit_neighs Calculate units in 1-neighborhood for each map unit. 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) 1999-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, 2, nargin)); wolffd@0: wolffd@0: if nargin<2, n=Inf; end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% Action wolffd@0: wolffd@0: % initialize wolffd@0: if issparse(Ne1), Ne = full(Ne1); else Ne = Ne1; end wolffd@0: clear Ne1 wolffd@0: [munits dummy] = size(Ne); wolffd@0: Ne(find(Ne==0)) = NaN; wolffd@0: for i=1:munits, Ne(i,i)=0; end wolffd@0: wolffd@0: % Calculate neighborhood distance for each unit using reflexsivity wolffd@0: % of neighborhood: wolffd@0: % let N1i be the 1-neighborhood set a unit i wolffd@0: % then N2i is the union of all map units, belonging to the wolffd@0: % 1-neighborhood of any unit j in N1i, not already in N1i wolffd@0: k=1; wolffd@0: if n>1, wolffd@0: fprintf(1,'Calculating neighborhood: 1 '); wolffd@0: N1 = Ne; wolffd@0: N1(find(N1~=1)) = 0; wolffd@0: end wolffd@0: while k1, fprintf(1,'\n'); end wolffd@0: wolffd@0: % finally replace all uncalculated distance values with Inf wolffd@0: Ne(find(isnan(Ne))) = Inf; wolffd@0: wolffd@0: return; wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% faster version? wolffd@0: wolffd@0: l = size(Ne1,1); Ne1([0:l-1]*(l+1)+1) = 1; Ne = full(Ne1); M0 = Ne1; k = 2; wolffd@0: while any(Ne(:)==0), M1=(M0*Ne1>0); Ne(find(M1-M0))=k; M0=M1; k=k+1; end wolffd@0: Ne([0:l-1]*(l+1)+1) = 0;