Daniel@0: function d=som_eucdist2(Data, Proto) Daniel@0: Daniel@0: %SOM_EUCDIST2 Calculates matrix of squared euclidean distances between set of vectors or map, data struct Daniel@0: % Daniel@0: % d=som_eucdist2(D, P) Daniel@0: % Daniel@0: % d=som_eucdist(sMap, sData); Daniel@0: % d=som_eucdist(sData, sMap); Daniel@0: % d=som_eucdist(sMap1, sMap2); Daniel@0: % d=som_eucdist(datamatrix1, datamatrix2); Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % D (matrix) size Nxd Daniel@0: % (struct) map or data struct Daniel@0: % P (matrix) size Pxd Daniel@0: % (struct) map or data struct Daniel@0: % d (matrix) distance matrix of size NxP Daniel@0: % Daniel@0: % IMPORTANT Daniel@0: % Daniel@0: % * Calculates _squared_ euclidean distances Daniel@0: % * Observe that the mask in the map struct is not taken into account while Daniel@0: % calculating the euclidean distance Daniel@0: % Daniel@0: % See also KNN, PDIST. Daniel@0: Daniel@0: % Contributed to SOM Toolbox 2.0, October 29th, 2000 by Johan Himberg Daniel@0: % Copyright (c) by Johan Himberg Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 2.0beta Johan 291000 Daniel@0: Daniel@0: %% Init %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: if isstruct(Data); Daniel@0: if isfield(Data,'type') & ischar(Data.type), Daniel@0: ; Daniel@0: else Daniel@0: error('Invalid map/data struct?'); Daniel@0: end Daniel@0: switch Data.type Daniel@0: case 'som_map' Daniel@0: data=Data.codebook; Daniel@0: case 'som_data' Daniel@0: data=Data.data; Daniel@0: end Daniel@0: else Daniel@0: % is already a matrix Daniel@0: data=Data; Daniel@0: end Daniel@0: Daniel@0: % Take prototype vectors from prototype struct Daniel@0: Daniel@0: if isstruct(Proto), Daniel@0: Daniel@0: if isfield(Proto,'type') & ischar(Proto.type), Daniel@0: ; Daniel@0: else Daniel@0: error('Invalid map/data struct?'); Daniel@0: end Daniel@0: switch Proto.type Daniel@0: case 'som_map' Daniel@0: proto=Proto.codebook; Daniel@0: case 'som_data' Daniel@0: proto=Proto.data; Daniel@0: end Daniel@0: else Daniel@0: % is already a matrix Daniel@0: proto=Proto; Daniel@0: end Daniel@0: Daniel@0: % Check that inputs are matrices Daniel@0: if ~vis_valuetype(proto,{'nxm'}) | ~vis_valuetype(data,{'nxm'}), Daniel@0: error('Prototype or data input not valid.') Daniel@0: end Daniel@0: Daniel@0: % Record data&proto sizes and check their dims Daniel@0: [N_data dim_data]=size(data); Daniel@0: [N_proto dim_proto]=size(proto); Daniel@0: if dim_proto ~= dim_data, Daniel@0: error('Data and prototype vector dimension does not match.'); Daniel@0: end Daniel@0: Daniel@0: % Calculate euclidean distances between classifiees and prototypes Daniel@0: d=distance(data,proto); Daniel@0: Daniel@0: %%%% Classification %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: function d=distance(X,Y); Daniel@0: Daniel@0: % Euclidean distance matrix between row vectors in X and Y Daniel@0: Daniel@0: U=~isnan(Y); Y(~U)=0; Daniel@0: V=~isnan(X); X(~V)=0; Daniel@0: d=abs(X.^2*U'+V*Y'.^2-2*X*Y');