wolffd@0: function U = som_umat(sMap, varargin) wolffd@0: wolffd@0: %SOM_UMAT Compute unified distance matrix of self-organizing map. wolffd@0: % wolffd@0: % U = som_umat(sMap, [argID, value, ...]) wolffd@0: % wolffd@0: % U = som_umat(sMap); wolffd@0: % U = som_umat(M,sTopol,'median','mask',[1 1 0 1]); wolffd@0: % wolffd@0: % Input and output arguments ([]'s are optional): wolffd@0: % sMap (struct) map struct or wolffd@0: % (matrix) the codebook matrix of the map wolffd@0: % [argID, (string) See below. The values which are unambiguous can wolffd@0: % value] (varies) be given without the preceeding argID. wolffd@0: % wolffd@0: % U (matrix) u-matrix of the self-organizing map wolffd@0: % wolffd@0: % Here are the valid argument IDs and corresponding values. The values which wolffd@0: % are unambiguous (marked with '*') can be given without the preceeding argID. wolffd@0: % 'mask' (vector) size dim x 1, weighting factors for different wolffd@0: % components (same as BMU search mask) wolffd@0: % 'msize' (vector) map grid size wolffd@0: % 'topol' *(struct) topology struct wolffd@0: % 'som_topol','sTopol' = 'topol' wolffd@0: % 'lattice' *(string) map lattice, 'hexa' or 'rect' wolffd@0: % 'mode' *(string) 'min','mean','median','max', default is 'median' wolffd@0: % wolffd@0: % NOTE! the U-matrix is always calculated for 'sheet'-shaped map and wolffd@0: % the map grid must be at most 2-dimensional. wolffd@0: % wolffd@0: % For more help, try 'type som_umat' or check out online documentation. wolffd@0: % See also SOM_SHOW, SOM_CPLANE. wolffd@0: wolffd@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % wolffd@0: % som_umat wolffd@0: % wolffd@0: % PURPOSE wolffd@0: % wolffd@0: % Computes the unified distance matrix of a SOM. wolffd@0: % wolffd@0: % SYNTAX wolffd@0: % wolffd@0: % U = som_umat(sM) wolffd@0: % U = som_umat(...,'argID',value,...) wolffd@0: % U = som_umat(...,value,...) wolffd@0: % wolffd@0: % DESCRIPTION wolffd@0: % wolffd@0: % Compute and return the unified distance matrix of a SOM. wolffd@0: % For example a case of 5x1 -sized map: wolffd@0: % m(1) m(2) m(3) m(4) m(5) wolffd@0: % where m(i) denotes one map unit. The u-matrix is a 9x1 vector: wolffd@0: % u(1) u(1,2) u(2) u(2,3) u(3) u(3,4) u(4) u(4,5) u(5) wolffd@0: % where u(i,j) is the distance between map units m(i) and m(j) wolffd@0: % and u(k) is the mean (or minimum, maximum or median) of the wolffd@0: % surrounding values, e.g. u(3) = (u(2,3) + u(3,4))/2. wolffd@0: % wolffd@0: % Note that the u-matrix is always calculated for 'sheet'-shaped map and wolffd@0: % the map grid must be at most 2-dimensional. wolffd@0: % wolffd@0: % REFERENCES wolffd@0: % wolffd@0: % Ultsch, A., Siemon, H.P., "Kohonen's Self-Organizing Feature Maps wolffd@0: % for Exploratory Data Analysis", in Proc. of INNC'90, wolffd@0: % International Neural Network Conference, Dordrecht, wolffd@0: % Netherlands, 1990, pp. 305-308. wolffd@0: % Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag, wolffd@0: % Berlin, 1995, pp. 117-119. wolffd@0: % Iivarinen, J., Kohonen, T., Kangas, J., Kaski, S., "Visualizing wolffd@0: % the Clusters on the Self-Organizing Map", in proceedings of wolffd@0: % Conference on Artificial Intelligence Research in Finland, wolffd@0: % Helsinki, Finland, 1994, pp. 122-126. wolffd@0: % Kraaijveld, M.A., Mao, J., Jain, A.K., "A Nonlinear Projection wolffd@0: % Method Based on Kohonen's Topology Preserving Maps", IEEE wolffd@0: % Transactions on Neural Networks, vol. 6, no. 3, 1995, pp. 548-559. wolffd@0: % wolffd@0: % REQUIRED INPUT ARGUMENTS wolffd@0: % wolffd@0: % sM (struct) SOM Toolbox struct or the codebook matrix of the map. wolffd@0: % (matrix) The matrix may be 3-dimensional in which case the first wolffd@0: % two dimensions are taken for the map grid dimensions (msize). wolffd@0: % wolffd@0: % OPTIONAL INPUT ARGUMENTS wolffd@0: % wolffd@0: % argID (string) Argument identifier string (see below). wolffd@0: % value (varies) Value for the argument (see below). wolffd@0: % wolffd@0: % The optional arguments are given as 'argID',value -pairs. If the wolffd@0: % value is unambiguous, it can be given without the preceeding argID. wolffd@0: % If an argument is given value multiple times, the last one is used. wolffd@0: % wolffd@0: % Below is the list of valid arguments: wolffd@0: % 'mask' (vector) mask to be used in calculating wolffd@0: % the interunit distances, size [dim 1]. Default is wolffd@0: % the one in sM (field sM.mask) or a vector of wolffd@0: % ones if only a codebook matrix was given. wolffd@0: % 'topol' (struct) topology of the map. Default is the one wolffd@0: % in sM (field sM.topol). wolffd@0: % 'sTopol','som_topol' (struct) = 'topol' wolffd@0: % 'msize' (vector) map grid dimensions wolffd@0: % 'lattice' (string) map lattice 'rect' or 'hexa' wolffd@0: % 'mode' (string) 'min', 'mean', 'median' or 'max' wolffd@0: % Map unit value computation method. In fact, wolffd@0: % eval-function is used to evaluate this, so wolffd@0: % you can give other computation methods as well. wolffd@0: % Default is 'median'. wolffd@0: % wolffd@0: % OUTPUT ARGUMENTS wolffd@0: % wolffd@0: % U (matrix) the unified distance matrix of the SOM wolffd@0: % size 2*n1-1 x 2*n2-1, where n1 = msize(1) and n2 = msize(2) wolffd@0: % wolffd@0: % EXAMPLES wolffd@0: % wolffd@0: % U = som_umat(sM); wolffd@0: % U = som_umat(sM.codebook,sM.topol,'median','mask',[1 1 0 1]); wolffd@0: % U = som_umat(rand(10,10,4),'hexa','rect'); wolffd@0: % wolffd@0: % SEE ALSO wolffd@0: % wolffd@0: % som_show show the selected component planes and the u-matrix wolffd@0: % som_cplane draw a 2D unified distance matrix wolffd@0: wolffd@0: % Copyright (c) 1997-2000 by the SOM toolbox programming team. wolffd@0: % http://www.cis.hut.fi/projects/somtoolbox/ wolffd@0: wolffd@0: % Version 1.0beta juuso 260997 wolffd@0: % Version 2.0beta juuso 151199, 151299, 200900 wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% check arguments wolffd@0: wolffd@0: error(nargchk(1, Inf, nargin)); % check no. of input arguments is correct wolffd@0: wolffd@0: % sMap wolffd@0: if isstruct(sMap), wolffd@0: M = sMap.codebook; wolffd@0: sTopol = sMap.topol; wolffd@0: mask = sMap.mask; wolffd@0: elseif isnumeric(sMap), wolffd@0: M = sMap; wolffd@0: si = size(M); wolffd@0: dim = si(end); wolffd@0: if length(si)>2, msize = si(1:end-1); wolffd@0: else msize = [si(1) 1]; wolffd@0: end wolffd@0: munits = prod(msize); wolffd@0: sTopol = som_set('som_topol','msize',msize,'lattice','rect','shape','sheet'); wolffd@0: mask = ones(dim,1); wolffd@0: M = reshape(M,[munits,dim]); wolffd@0: end wolffd@0: mode = 'median'; wolffd@0: wolffd@0: % varargin wolffd@0: i=1; wolffd@0: while i<=length(varargin), wolffd@0: argok = 1; wolffd@0: if ischar(varargin{i}), wolffd@0: switch varargin{i}, wolffd@0: % argument IDs wolffd@0: case 'mask', i=i+1; mask = varargin{i}; wolffd@0: case 'msize', i=i+1; sTopol.msize = varargin{i}; wolffd@0: case 'lattice', i=i+1; sTopol.lattice = varargin{i}; wolffd@0: case {'topol','som_topol','sTopol'}, i=i+1; sTopol = varargin{i}; wolffd@0: case 'mode', i=i+1; mode = varargin{i}; wolffd@0: % unambiguous values wolffd@0: case {'hexa','rect'}, sTopol.lattice = varargin{i}; wolffd@0: case {'min','mean','median','max'}, mode = varargin{i}; wolffd@0: otherwise argok=0; wolffd@0: end wolffd@0: elseif isstruct(varargin{i}) & isfield(varargin{i},'type'), wolffd@0: switch varargin{i}(1).type, wolffd@0: case 'som_topol', sTopol = varargin{i}; wolffd@0: case 'som_map', sTopol = varargin{i}.topol; wolffd@0: otherwise argok=0; wolffd@0: end wolffd@0: else wolffd@0: argok = 0; wolffd@0: end wolffd@0: if ~argok, wolffd@0: disp(['(som_umat) Ignoring invalid argument #' num2str(i+1)]); wolffd@0: end wolffd@0: i = i+1; wolffd@0: end wolffd@0: wolffd@0: % check wolffd@0: [munits dim] = size(M); wolffd@0: if prod(sTopol.msize)~=munits, wolffd@0: error('Map grid size does not match the number of map units.') wolffd@0: end wolffd@0: if length(sTopol.msize)>2, wolffd@0: error('Can only handle 1- and 2-dimensional map grids.') wolffd@0: end wolffd@0: if prod(sTopol.msize)==1, wolffd@0: warning('Only one codebook vector.'); U = []; return; wolffd@0: end wolffd@0: if ~strcmp(sTopol.shape,'sheet'), wolffd@0: disp(['The ' sTopol.shape ' shape of the map ignored. Using sheet instead.']); wolffd@0: end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% initialize variables wolffd@0: wolffd@0: y = sTopol.msize(1); wolffd@0: x = sTopol.msize(2); wolffd@0: lattice = sTopol.lattice; wolffd@0: shape = sTopol.shape; wolffd@0: M = reshape(M,[y x dim]); wolffd@0: wolffd@0: ux = 2 * x - 1; wolffd@0: uy = 2 * y - 1; wolffd@0: U = zeros(uy, ux); wolffd@0: wolffd@0: calc = sprintf('%s(a)',mode); wolffd@0: wolffd@0: if size(mask,2)>1, mask = mask'; end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% u-matrix computation wolffd@0: wolffd@0: % distances between map units wolffd@0: wolffd@0: if strcmp(lattice, 'rect'), % rectangular lattice wolffd@0: wolffd@0: for j=1:y, for i=1:x, wolffd@0: if i1, wolffd@0: dz = (M(j,i,:) - M(j+1,i-1,:)).^2; wolffd@0: U(2*j,2*i-2) = sqrt(mask'*dz(:)); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: end wolffd@0: wolffd@0: % values on the units wolffd@0: wolffd@0: if (uy == 1 | ux == 1), wolffd@0: % in 1-D case, mean is equal to median wolffd@0: wolffd@0: ma = max([ux uy]); wolffd@0: for i = 1:2:ma, wolffd@0: if i>1 & i1 & j>1 & i1 & i1 & i1 & j1 & j1 & j>1 & i1 & i1 & i1 & j1 & j 0, U = U / ma; end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: wolffd@0: wolffd@0: