annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_umat.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function U = som_umat(sMap, varargin)
Daniel@0 2
Daniel@0 3 %SOM_UMAT Compute unified distance matrix of self-organizing map.
Daniel@0 4 %
Daniel@0 5 % U = som_umat(sMap, [argID, value, ...])
Daniel@0 6 %
Daniel@0 7 % U = som_umat(sMap);
Daniel@0 8 % U = som_umat(M,sTopol,'median','mask',[1 1 0 1]);
Daniel@0 9 %
Daniel@0 10 % Input and output arguments ([]'s are optional):
Daniel@0 11 % sMap (struct) map struct or
Daniel@0 12 % (matrix) the codebook matrix of the map
Daniel@0 13 % [argID, (string) See below. The values which are unambiguous can
Daniel@0 14 % value] (varies) be given without the preceeding argID.
Daniel@0 15 %
Daniel@0 16 % U (matrix) u-matrix of the self-organizing map
Daniel@0 17 %
Daniel@0 18 % Here are the valid argument IDs and corresponding values. The values which
Daniel@0 19 % are unambiguous (marked with '*') can be given without the preceeding argID.
Daniel@0 20 % 'mask' (vector) size dim x 1, weighting factors for different
Daniel@0 21 % components (same as BMU search mask)
Daniel@0 22 % 'msize' (vector) map grid size
Daniel@0 23 % 'topol' *(struct) topology struct
Daniel@0 24 % 'som_topol','sTopol' = 'topol'
Daniel@0 25 % 'lattice' *(string) map lattice, 'hexa' or 'rect'
Daniel@0 26 % 'mode' *(string) 'min','mean','median','max', default is 'median'
Daniel@0 27 %
Daniel@0 28 % NOTE! the U-matrix is always calculated for 'sheet'-shaped map and
Daniel@0 29 % the map grid must be at most 2-dimensional.
Daniel@0 30 %
Daniel@0 31 % For more help, try 'type som_umat' or check out online documentation.
Daniel@0 32 % See also SOM_SHOW, SOM_CPLANE.
Daniel@0 33
Daniel@0 34 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 35 %
Daniel@0 36 % som_umat
Daniel@0 37 %
Daniel@0 38 % PURPOSE
Daniel@0 39 %
Daniel@0 40 % Computes the unified distance matrix of a SOM.
Daniel@0 41 %
Daniel@0 42 % SYNTAX
Daniel@0 43 %
Daniel@0 44 % U = som_umat(sM)
Daniel@0 45 % U = som_umat(...,'argID',value,...)
Daniel@0 46 % U = som_umat(...,value,...)
Daniel@0 47 %
Daniel@0 48 % DESCRIPTION
Daniel@0 49 %
Daniel@0 50 % Compute and return the unified distance matrix of a SOM.
Daniel@0 51 % For example a case of 5x1 -sized map:
Daniel@0 52 % m(1) m(2) m(3) m(4) m(5)
Daniel@0 53 % where m(i) denotes one map unit. The u-matrix is a 9x1 vector:
Daniel@0 54 % u(1) u(1,2) u(2) u(2,3) u(3) u(3,4) u(4) u(4,5) u(5)
Daniel@0 55 % where u(i,j) is the distance between map units m(i) and m(j)
Daniel@0 56 % and u(k) is the mean (or minimum, maximum or median) of the
Daniel@0 57 % surrounding values, e.g. u(3) = (u(2,3) + u(3,4))/2.
Daniel@0 58 %
Daniel@0 59 % Note that the u-matrix is always calculated for 'sheet'-shaped map and
Daniel@0 60 % the map grid must be at most 2-dimensional.
Daniel@0 61 %
Daniel@0 62 % REFERENCES
Daniel@0 63 %
Daniel@0 64 % Ultsch, A., Siemon, H.P., "Kohonen's Self-Organizing Feature Maps
Daniel@0 65 % for Exploratory Data Analysis", in Proc. of INNC'90,
Daniel@0 66 % International Neural Network Conference, Dordrecht,
Daniel@0 67 % Netherlands, 1990, pp. 305-308.
Daniel@0 68 % Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag,
Daniel@0 69 % Berlin, 1995, pp. 117-119.
Daniel@0 70 % Iivarinen, J., Kohonen, T., Kangas, J., Kaski, S., "Visualizing
Daniel@0 71 % the Clusters on the Self-Organizing Map", in proceedings of
Daniel@0 72 % Conference on Artificial Intelligence Research in Finland,
Daniel@0 73 % Helsinki, Finland, 1994, pp. 122-126.
Daniel@0 74 % Kraaijveld, M.A., Mao, J., Jain, A.K., "A Nonlinear Projection
Daniel@0 75 % Method Based on Kohonen's Topology Preserving Maps", IEEE
Daniel@0 76 % Transactions on Neural Networks, vol. 6, no. 3, 1995, pp. 548-559.
Daniel@0 77 %
Daniel@0 78 % REQUIRED INPUT ARGUMENTS
Daniel@0 79 %
Daniel@0 80 % sM (struct) SOM Toolbox struct or the codebook matrix of the map.
Daniel@0 81 % (matrix) The matrix may be 3-dimensional in which case the first
Daniel@0 82 % two dimensions are taken for the map grid dimensions (msize).
Daniel@0 83 %
Daniel@0 84 % OPTIONAL INPUT ARGUMENTS
Daniel@0 85 %
Daniel@0 86 % argID (string) Argument identifier string (see below).
Daniel@0 87 % value (varies) Value for the argument (see below).
Daniel@0 88 %
Daniel@0 89 % The optional arguments are given as 'argID',value -pairs. If the
Daniel@0 90 % value is unambiguous, it can be given without the preceeding argID.
Daniel@0 91 % If an argument is given value multiple times, the last one is used.
Daniel@0 92 %
Daniel@0 93 % Below is the list of valid arguments:
Daniel@0 94 % 'mask' (vector) mask to be used in calculating
Daniel@0 95 % the interunit distances, size [dim 1]. Default is
Daniel@0 96 % the one in sM (field sM.mask) or a vector of
Daniel@0 97 % ones if only a codebook matrix was given.
Daniel@0 98 % 'topol' (struct) topology of the map. Default is the one
Daniel@0 99 % in sM (field sM.topol).
Daniel@0 100 % 'sTopol','som_topol' (struct) = 'topol'
Daniel@0 101 % 'msize' (vector) map grid dimensions
Daniel@0 102 % 'lattice' (string) map lattice 'rect' or 'hexa'
Daniel@0 103 % 'mode' (string) 'min', 'mean', 'median' or 'max'
Daniel@0 104 % Map unit value computation method. In fact,
Daniel@0 105 % eval-function is used to evaluate this, so
Daniel@0 106 % you can give other computation methods as well.
Daniel@0 107 % Default is 'median'.
Daniel@0 108 %
Daniel@0 109 % OUTPUT ARGUMENTS
Daniel@0 110 %
Daniel@0 111 % U (matrix) the unified distance matrix of the SOM
Daniel@0 112 % size 2*n1-1 x 2*n2-1, where n1 = msize(1) and n2 = msize(2)
Daniel@0 113 %
Daniel@0 114 % EXAMPLES
Daniel@0 115 %
Daniel@0 116 % U = som_umat(sM);
Daniel@0 117 % U = som_umat(sM.codebook,sM.topol,'median','mask',[1 1 0 1]);
Daniel@0 118 % U = som_umat(rand(10,10,4),'hexa','rect');
Daniel@0 119 %
Daniel@0 120 % SEE ALSO
Daniel@0 121 %
Daniel@0 122 % som_show show the selected component planes and the u-matrix
Daniel@0 123 % som_cplane draw a 2D unified distance matrix
Daniel@0 124
Daniel@0 125 % Copyright (c) 1997-2000 by the SOM toolbox programming team.
Daniel@0 126 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 127
Daniel@0 128 % Version 1.0beta juuso 260997
Daniel@0 129 % Version 2.0beta juuso 151199, 151299, 200900
Daniel@0 130
Daniel@0 131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 132 %% check arguments
Daniel@0 133
Daniel@0 134 error(nargchk(1, Inf, nargin)); % check no. of input arguments is correct
Daniel@0 135
Daniel@0 136 % sMap
Daniel@0 137 if isstruct(sMap),
Daniel@0 138 M = sMap.codebook;
Daniel@0 139 sTopol = sMap.topol;
Daniel@0 140 mask = sMap.mask;
Daniel@0 141 elseif isnumeric(sMap),
Daniel@0 142 M = sMap;
Daniel@0 143 si = size(M);
Daniel@0 144 dim = si(end);
Daniel@0 145 if length(si)>2, msize = si(1:end-1);
Daniel@0 146 else msize = [si(1) 1];
Daniel@0 147 end
Daniel@0 148 munits = prod(msize);
Daniel@0 149 sTopol = som_set('som_topol','msize',msize,'lattice','rect','shape','sheet');
Daniel@0 150 mask = ones(dim,1);
Daniel@0 151 M = reshape(M,[munits,dim]);
Daniel@0 152 end
Daniel@0 153 mode = 'median';
Daniel@0 154
Daniel@0 155 % varargin
Daniel@0 156 i=1;
Daniel@0 157 while i<=length(varargin),
Daniel@0 158 argok = 1;
Daniel@0 159 if ischar(varargin{i}),
Daniel@0 160 switch varargin{i},
Daniel@0 161 % argument IDs
Daniel@0 162 case 'mask', i=i+1; mask = varargin{i};
Daniel@0 163 case 'msize', i=i+1; sTopol.msize = varargin{i};
Daniel@0 164 case 'lattice', i=i+1; sTopol.lattice = varargin{i};
Daniel@0 165 case {'topol','som_topol','sTopol'}, i=i+1; sTopol = varargin{i};
Daniel@0 166 case 'mode', i=i+1; mode = varargin{i};
Daniel@0 167 % unambiguous values
Daniel@0 168 case {'hexa','rect'}, sTopol.lattice = varargin{i};
Daniel@0 169 case {'min','mean','median','max'}, mode = varargin{i};
Daniel@0 170 otherwise argok=0;
Daniel@0 171 end
Daniel@0 172 elseif isstruct(varargin{i}) & isfield(varargin{i},'type'),
Daniel@0 173 switch varargin{i}(1).type,
Daniel@0 174 case 'som_topol', sTopol = varargin{i};
Daniel@0 175 case 'som_map', sTopol = varargin{i}.topol;
Daniel@0 176 otherwise argok=0;
Daniel@0 177 end
Daniel@0 178 else
Daniel@0 179 argok = 0;
Daniel@0 180 end
Daniel@0 181 if ~argok,
Daniel@0 182 disp(['(som_umat) Ignoring invalid argument #' num2str(i+1)]);
Daniel@0 183 end
Daniel@0 184 i = i+1;
Daniel@0 185 end
Daniel@0 186
Daniel@0 187 % check
Daniel@0 188 [munits dim] = size(M);
Daniel@0 189 if prod(sTopol.msize)~=munits,
Daniel@0 190 error('Map grid size does not match the number of map units.')
Daniel@0 191 end
Daniel@0 192 if length(sTopol.msize)>2,
Daniel@0 193 error('Can only handle 1- and 2-dimensional map grids.')
Daniel@0 194 end
Daniel@0 195 if prod(sTopol.msize)==1,
Daniel@0 196 warning('Only one codebook vector.'); U = []; return;
Daniel@0 197 end
Daniel@0 198 if ~strcmp(sTopol.shape,'sheet'),
Daniel@0 199 disp(['The ' sTopol.shape ' shape of the map ignored. Using sheet instead.']);
Daniel@0 200 end
Daniel@0 201
Daniel@0 202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 203 %% initialize variables
Daniel@0 204
Daniel@0 205 y = sTopol.msize(1);
Daniel@0 206 x = sTopol.msize(2);
Daniel@0 207 lattice = sTopol.lattice;
Daniel@0 208 shape = sTopol.shape;
Daniel@0 209 M = reshape(M,[y x dim]);
Daniel@0 210
Daniel@0 211 ux = 2 * x - 1;
Daniel@0 212 uy = 2 * y - 1;
Daniel@0 213 U = zeros(uy, ux);
Daniel@0 214
Daniel@0 215 calc = sprintf('%s(a)',mode);
Daniel@0 216
Daniel@0 217 if size(mask,2)>1, mask = mask'; end
Daniel@0 218
Daniel@0 219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 220 %% u-matrix computation
Daniel@0 221
Daniel@0 222 % distances between map units
Daniel@0 223
Daniel@0 224 if strcmp(lattice, 'rect'), % rectangular lattice
Daniel@0 225
Daniel@0 226 for j=1:y, for i=1:x,
Daniel@0 227 if i<x,
Daniel@0 228 dx = (M(j,i,:) - M(j,i+1,:)).^2; % horizontal
Daniel@0 229 U(2*j-1,2*i) = sqrt(mask'*dx(:));
Daniel@0 230 end
Daniel@0 231 if j<y,
Daniel@0 232 dy = (M(j,i,:) - M(j+1,i,:)).^2; % vertical
Daniel@0 233 U(2*j,2*i-1) = sqrt(mask'*dy(:));
Daniel@0 234 end
Daniel@0 235 if j<y & i<x,
Daniel@0 236 dz1 = (M(j,i,:) - M(j+1,i+1,:)).^2; % diagonals
Daniel@0 237 dz2 = (M(j+1,i,:) - M(j,i+1,:)).^2;
Daniel@0 238 U(2*j,2*i) = (sqrt(mask'*dz1(:))+sqrt(mask'*dz2(:)))/(2 * sqrt(2));
Daniel@0 239 end
Daniel@0 240 end
Daniel@0 241 end
Daniel@0 242
Daniel@0 243 elseif strcmp(lattice, 'hexa') % hexagonal lattice
Daniel@0 244
Daniel@0 245 for j=1:y,
Daniel@0 246 for i=1:x,
Daniel@0 247 if i<x,
Daniel@0 248 dx = (M(j,i,:) - M(j,i+1,:)).^2; % horizontal
Daniel@0 249 U(2*j-1,2*i) = sqrt(mask'*dx(:));
Daniel@0 250 end
Daniel@0 251
Daniel@0 252 if j<y, % diagonals
Daniel@0 253 dy = (M(j,i,:) - M(j+1,i,:)).^2;
Daniel@0 254 U(2*j,2*i-1) = sqrt(mask'*dy(:));
Daniel@0 255
Daniel@0 256 if rem(j,2)==0 & i<x,
Daniel@0 257 dz= (M(j,i,:) - M(j+1,i+1,:)).^2;
Daniel@0 258 U(2*j,2*i) = sqrt(mask'*dz(:));
Daniel@0 259 elseif rem(j,2)==1 & i>1,
Daniel@0 260 dz = (M(j,i,:) - M(j+1,i-1,:)).^2;
Daniel@0 261 U(2*j,2*i-2) = sqrt(mask'*dz(:));
Daniel@0 262 end
Daniel@0 263 end
Daniel@0 264 end
Daniel@0 265 end
Daniel@0 266
Daniel@0 267 end
Daniel@0 268
Daniel@0 269 % values on the units
Daniel@0 270
Daniel@0 271 if (uy == 1 | ux == 1),
Daniel@0 272 % in 1-D case, mean is equal to median
Daniel@0 273
Daniel@0 274 ma = max([ux uy]);
Daniel@0 275 for i = 1:2:ma,
Daniel@0 276 if i>1 & i<ma,
Daniel@0 277 a = [U(i-1) U(i+1)];
Daniel@0 278 U(i) = eval(calc);
Daniel@0 279 elseif i==1, U(i) = U(i+1);
Daniel@0 280 else U(i) = U(i-1); % i==ma
Daniel@0 281 end
Daniel@0 282 end
Daniel@0 283
Daniel@0 284 elseif strcmp(lattice, 'rect')
Daniel@0 285
Daniel@0 286 for j=1:2:uy,
Daniel@0 287 for i=1:2:ux,
Daniel@0 288 if i>1 & j>1 & i<ux & j<uy, % middle part of the map
Daniel@0 289 a = [U(j,i-1) U(j,i+1) U(j-1,i) U(j+1,i)];
Daniel@0 290 elseif j==1 & i>1 & i<ux, % upper edge
Daniel@0 291 a = [U(j,i-1) U(j,i+1) U(j+1,i)];
Daniel@0 292 elseif j==uy & i>1 & i<ux, % lower edge
Daniel@0 293 a = [U(j,i-1) U(j,i+1) U(j-1,i)];
Daniel@0 294 elseif i==1 & j>1 & j<uy, % left edge
Daniel@0 295 a = [U(j,i+1) U(j-1,i) U(j+1,i)];
Daniel@0 296 elseif i==ux & j>1 & j<uy, % right edge
Daniel@0 297 a = [U(j,i-1) U(j-1,i) U(j+1,i)];
Daniel@0 298 elseif i==1 & j==1, % top left corner
Daniel@0 299 a = [U(j,i+1) U(j+1,i)];
Daniel@0 300 elseif i==ux & j==1, % top right corner
Daniel@0 301 a = [U(j,i-1) U(j+1,i)];
Daniel@0 302 elseif i==1 & j==uy, % bottom left corner
Daniel@0 303 a = [U(j,i+1) U(j-1,i)];
Daniel@0 304 elseif i==ux & j==uy, % bottom right corner
Daniel@0 305 a = [U(j,i-1) U(j-1,i)];
Daniel@0 306 else
Daniel@0 307 a = 0;
Daniel@0 308 end
Daniel@0 309 U(j,i) = eval(calc);
Daniel@0 310 end
Daniel@0 311 end
Daniel@0 312
Daniel@0 313 elseif strcmp(lattice, 'hexa')
Daniel@0 314
Daniel@0 315 for j=1:2:uy,
Daniel@0 316 for i=1:2:ux,
Daniel@0 317 if i>1 & j>1 & i<ux & j<uy, % middle part of the map
Daniel@0 318 a = [U(j,i-1) U(j,i+1)];
Daniel@0 319 if rem(j-1,4)==0, a = [a, U(j-1,i-1) U(j-1,i) U(j+1,i-1) U(j+1,i)];
Daniel@0 320 else a = [a, U(j-1,i) U(j-1,i+1) U(j+1,i) U(j+1,i+1)]; end
Daniel@0 321 elseif j==1 & i>1 & i<ux, % upper edge
Daniel@0 322 a = [U(j,i-1) U(j,i+1) U(j+1,i-1) U(j+1,i)];
Daniel@0 323 elseif j==uy & i>1 & i<ux, % lower edge
Daniel@0 324 a = [U(j,i-1) U(j,i+1)];
Daniel@0 325 if rem(j-1,4)==0, a = [a, U(j-1,i-1) U(j-1,i)];
Daniel@0 326 else a = [a, U(j-1,i) U(j-1,i+1)]; end
Daniel@0 327 elseif i==1 & j>1 & j<uy, % left edge
Daniel@0 328 a = U(j,i+1);
Daniel@0 329 if rem(j-1,4)==0, a = [a, U(j-1,i) U(j+1,i)];
Daniel@0 330 else a = [a, U(j-1,i) U(j-1,i+1) U(j+1,i) U(j+1,i+1)]; end
Daniel@0 331 elseif i==ux & j>1 & j<uy, % right edge
Daniel@0 332 a = U(j,i-1);
Daniel@0 333 if rem(j-1,4)==0, a=[a, U(j-1,i) U(j-1,i-1) U(j+1,i) U(j+1,i-1)];
Daniel@0 334 else a = [a, U(j-1,i) U(j+1,i)]; end
Daniel@0 335 elseif i==1 & j==1, % top left corner
Daniel@0 336 a = [U(j,i+1) U(j+1,i)];
Daniel@0 337 elseif i==ux & j==1, % top right corner
Daniel@0 338 a = [U(j,i-1) U(j+1,i-1) U(j+1,i)];
Daniel@0 339 elseif i==1 & j==uy, % bottom left corner
Daniel@0 340 if rem(j-1,4)==0, a = [U(j,i+1) U(j-1,i)];
Daniel@0 341 else a = [U(j,i+1) U(j-1,i) U(j-1,i+1)]; end
Daniel@0 342 elseif i==ux & j==uy, % bottom right corner
Daniel@0 343 if rem(j-1,4)==0, a = [U(j,i-1) U(j-1,i) U(j-1,i-1)];
Daniel@0 344 else a = [U(j,i-1) U(j-1,i)]; end
Daniel@0 345 else
Daniel@0 346 a=0;
Daniel@0 347 end
Daniel@0 348 U(j,i) = eval(calc);
Daniel@0 349 end
Daniel@0 350 end
Daniel@0 351 end
Daniel@0 352
Daniel@0 353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 354 %% normalization between [0,1]
Daniel@0 355
Daniel@0 356 % U = U - min(min(U));
Daniel@0 357 % ma = max(max(U)); if ma > 0, U = U / ma; end
Daniel@0 358
Daniel@0 359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 360
Daniel@0 361
Daniel@0 362