annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_eucdist2.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function d=som_eucdist2(Data, Proto)
wolffd@0 2
wolffd@0 3 %SOM_EUCDIST2 Calculates matrix of squared euclidean distances between set of vectors or map, data struct
wolffd@0 4 %
wolffd@0 5 % d=som_eucdist2(D, P)
wolffd@0 6 %
wolffd@0 7 % d=som_eucdist(sMap, sData);
wolffd@0 8 % d=som_eucdist(sData, sMap);
wolffd@0 9 % d=som_eucdist(sMap1, sMap2);
wolffd@0 10 % d=som_eucdist(datamatrix1, datamatrix2);
wolffd@0 11 %
wolffd@0 12 % Input and output arguments ([]'s are optional):
wolffd@0 13 % D (matrix) size Nxd
wolffd@0 14 % (struct) map or data struct
wolffd@0 15 % P (matrix) size Pxd
wolffd@0 16 % (struct) map or data struct
wolffd@0 17 % d (matrix) distance matrix of size NxP
wolffd@0 18 %
wolffd@0 19 % IMPORTANT
wolffd@0 20 %
wolffd@0 21 % * Calculates _squared_ euclidean distances
wolffd@0 22 % * Observe that the mask in the map struct is not taken into account while
wolffd@0 23 % calculating the euclidean distance
wolffd@0 24 %
wolffd@0 25 % See also KNN, PDIST.
wolffd@0 26
wolffd@0 27 % Contributed to SOM Toolbox 2.0, October 29th, 2000 by Johan Himberg
wolffd@0 28 % Copyright (c) by Johan Himberg
wolffd@0 29 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 30
wolffd@0 31 % Version 2.0beta Johan 291000
wolffd@0 32
wolffd@0 33 %% Init %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 34
wolffd@0 35 if isstruct(Data);
wolffd@0 36 if isfield(Data,'type') & ischar(Data.type),
wolffd@0 37 ;
wolffd@0 38 else
wolffd@0 39 error('Invalid map/data struct?');
wolffd@0 40 end
wolffd@0 41 switch Data.type
wolffd@0 42 case 'som_map'
wolffd@0 43 data=Data.codebook;
wolffd@0 44 case 'som_data'
wolffd@0 45 data=Data.data;
wolffd@0 46 end
wolffd@0 47 else
wolffd@0 48 % is already a matrix
wolffd@0 49 data=Data;
wolffd@0 50 end
wolffd@0 51
wolffd@0 52 % Take prototype vectors from prototype struct
wolffd@0 53
wolffd@0 54 if isstruct(Proto),
wolffd@0 55
wolffd@0 56 if isfield(Proto,'type') & ischar(Proto.type),
wolffd@0 57 ;
wolffd@0 58 else
wolffd@0 59 error('Invalid map/data struct?');
wolffd@0 60 end
wolffd@0 61 switch Proto.type
wolffd@0 62 case 'som_map'
wolffd@0 63 proto=Proto.codebook;
wolffd@0 64 case 'som_data'
wolffd@0 65 proto=Proto.data;
wolffd@0 66 end
wolffd@0 67 else
wolffd@0 68 % is already a matrix
wolffd@0 69 proto=Proto;
wolffd@0 70 end
wolffd@0 71
wolffd@0 72 % Check that inputs are matrices
wolffd@0 73 if ~vis_valuetype(proto,{'nxm'}) | ~vis_valuetype(data,{'nxm'}),
wolffd@0 74 error('Prototype or data input not valid.')
wolffd@0 75 end
wolffd@0 76
wolffd@0 77 % Record data&proto sizes and check their dims
wolffd@0 78 [N_data dim_data]=size(data);
wolffd@0 79 [N_proto dim_proto]=size(proto);
wolffd@0 80 if dim_proto ~= dim_data,
wolffd@0 81 error('Data and prototype vector dimension does not match.');
wolffd@0 82 end
wolffd@0 83
wolffd@0 84 % Calculate euclidean distances between classifiees and prototypes
wolffd@0 85 d=distance(data,proto);
wolffd@0 86
wolffd@0 87 %%%% Classification %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 88 function d=distance(X,Y);
wolffd@0 89
wolffd@0 90 % Euclidean distance matrix between row vectors in X and Y
wolffd@0 91
wolffd@0 92 U=~isnan(Y); Y(~U)=0;
wolffd@0 93 V=~isnan(X); X(~V)=0;
wolffd@0 94 d=abs(X.^2*U'+V*Y'.^2-2*X*Y');