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