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