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