function U = som_umat(M, varargin) %SOM_UMAT Compute unified distance matrix of self-organizing map. % % U = som_umat(M, [lattice], [shape], [mode]) % % ARGUMENTS ([]'s are optional) % % M (struct or matrix) self-organizing map struct or the % the corresponding codebook matrix, size n1 x n2 x dim % [lattice] (string) map lattice, 'rect' or 'hexa', default is 'rect' % [shape] (string) map shape, 'rect' or 'cyl' or 'toroid', % default is 'rect' % [mode] (string) method used for determining unit center value, % 'mean' or 'median', default is 'mean' % RETURNS % % U (matrix) u-matrix of the self-organizing map, % size (2*n1-1 x 2*n2-1) % % This function calculates and returns the unified distance matrix % of a SOM. For example a case of 5x1 -sized map: % m(1) m(2) m(3) m(4) m(5) % where m(i) denotes one map unit. The u-matrix is a 9x1 vector: % u(1) u(1,2) u(2) u(2,3) u(3) u(3,4) u(4) u(4,5) u(5) % where u(i,j) is the distance between map units m(i) and m(j) % and u(k) is the mean (or the median) of the surrounding values, % e.g. u(3) = (u(2,3) + u(3,4))/2. Examples of usage: % % U = som_umat(sM); % U = som_umat(sM.codebook(:,:,[1 3:5]),sM.lattice,sM.shape,'median'); % U = som_umat(rand(10,10,4),'hexa','rect'); % % NOTE: the dimension of the map grid may be 2 at most. % NOTE: the mask field of the M matrix is ignored. % % See also: SOM_SHOW, SOM_PLANEU. % Copyright (c) 1997 by the SOM toolbox programming team. % http://www.cis.hut.fi/projects/somtoolbox/ % Version 1.0beta juuso / ecco 210797, ecco 190997, juuso 260997 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% check arguments error(nargchk(1, 4, nargin)); % check no. of input arguments is correct % default values lattice = 'rect'; shape = 'rect'; m = 1; % M if isstruct(M), % som struct lattice = M.lattice; shape = M.shape; M = M.codebook; end if length(size(M)) > 3 error('Cannot compute u-matrices for map whose dimensions are higher than 2.'); end % shape, lattice, mode for i=1:(nargin-1), switch varargin{i}, case 'rect', if i==2, lattice = 'rect'; else shape = 'rect'; end case 'hexa', lattice = 'hexa'; case 'cyl', shape = 'cyl'; case 'toroid', shape = 'toroid'; case 'mean', m = 1; case 'median', m = 2; otherwise, error(['Unrecognized input argument: ', varargin{i}]); end end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% initialize variables si = size(M); x = si(1); if length(si) == 2 dim = si(2); y = 1; else y = si(2); dim = si(3); end if x == 1 & y == 1, warning('Only one codebook vector'); U = []; return; end ux = 2 * x - 1; uy = 2 * y - 1; U = zeros(ux, uy); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% u-matrix computation if strcmp(lattice, 'rect'), if ~strcmp(shape,'rect'), warning(['Using rectangular shape instead of ', shape, '.']); end for j=1:y, for i=1:x, if i1, dy = sum((M(i,j,:) - M(i-1,j+1,:)).^2); U(2*i-2,2*j) = sqrt(dy); else end end if j1 & i1 & j>1 & i1 & i1 & i1 & j1 & j1 & j>1 & i1 & i1 & i1 & j1 & j 0, U = U / ma; end