Home > data-import > supporting_functions > lib > distance.m

distance

PURPOSE ^

DISTANCE - computes Euclidean distance matrix

SYNOPSIS ^

function d = distance(a,b)

DESCRIPTION ^

 DISTANCE - computes Euclidean distance matrix

 E = distance(A,B)

    A - (DxM) matrix 
    B - (DxN) matrix

 Returns:
    E - (MxN) Euclidean distances between vectors in A and B


 Description : 
    This fully vectorized (VERY FAST!) m-file computes the 
    Euclidean distance between two vectors by:

                 ||A-B|| = sqrt ( ||A||^2 + ||B||^2 - 2*A.B )

 Example : 
    A = rand(400,100); B = rand(400,200);
    d = distance(A,B);

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function d = distance(a,b)
0002 % DISTANCE - computes Euclidean distance matrix
0003 %
0004 % E = distance(A,B)
0005 %
0006 %    A - (DxM) matrix
0007 %    B - (DxN) matrix
0008 %
0009 % Returns:
0010 %    E - (MxN) Euclidean distances between vectors in A and B
0011 %
0012 %
0013 % Description :
0014 %    This fully vectorized (VERY FAST!) m-file computes the
0015 %    Euclidean distance between two vectors by:
0016 %
0017 %                 ||A-B|| = sqrt ( ||A||^2 + ||B||^2 - 2*A.B )
0018 %
0019 % Example :
0020 %    A = rand(400,100); B = rand(400,200);
0021 %    d = distance(A,B);
0022 
0023 % Author   : Roland Bunschoten
0024 %            University of Amsterdam
0025 %            Intelligent Autonomous Systems (IAS) group
0026 %            Kruislaan 403  1098 SJ Amsterdam
0027 %            tel.(+31)20-5257524
0028 %            bunschot@wins.uva.nl
0029 % Last Rev : Oct 29 16:35:48 MET DST 1999
0030 % Tested   : PC Matlab v5.2 and Solaris Matlab v5.3
0031 % Thanx    : Nikos Vlassis
0032 
0033 % Copyright notice: You are free to modify, extend and distribute
0034 %    this code granted that the author of the original code is
0035 %    mentioned as the original author of the code.
0036 
0037 if (nargin ~= 2)
0038    error('Not enough input arguments');
0039 end
0040 
0041 if (size(a,1) ~= size(b,1))
0042    error('A and B should be of same dimensionality');
0043 end
0044 
0045 aa=sum(a.*a,1); bb=sum(b.*b,1); ab=a'*b; 
0046 d = sqrt(abs(repmat(aa',[1 size(bb,2)]) + repmat(bb,[size(aa,2) 1]) - 2*ab));

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005