Switch to unified view

a b/functions/functions_Classifiers/fastEuclideanDistance.m
1
function d = fastEuclideanDistance(a,b)
2
% DISTANCE - computes Euclidean distance matrix
3
%
4
% E = distance(A,B)
5
%
6
%    A - (DxM) matrix 
7
%    B - (DxN) matrix
8
%
9
% Returns:
10
%    E - (MxN) Euclidean distances between vectors in A and B
11
%
12
%
13
% Description : 
14
%    This fully vectorized (VERY FAST!) m-file computes the 
15
%    Euclidean distance between two vectors by:
16
%
17
%                 ||A-B|| = sqrt ( ||A||^2 + ||B||^2 - 2*A.B )
18
%
19
% Example : 
20
%    A = rand(400,100); B = rand(400,200);
21
%    d = distance(A,B);
22
23
% Author   : Roland Bunschoten
24
%            University of Amsterdam
25
%            Intelligent Autonomous Systems (IAS) group
26
%            Kruislaan 403  1098 SJ Amsterdam
27
%            tel.(+31)20-5257524
28
%            bunschot@wins.uva.nl
29
% Last Rev : Oct 29 16:35:48 MET DST 1999
30
% Tested   : PC Matlab v5.2 and Solaris Matlab v5.3
31
% Thanx    : Nikos Vlassis
32
33
% Copyright notice: You are free to modify, extend and distribute 
34
%    this code granted that the author of the original code is 
35
%    mentioned as the original author of the code.
36
37
if (nargin ~= 2)
38
   error('Not enough input arguments');
39
end
40
41
if (size(a,1) ~= size(b,1))
42
   error('A and B should be of same dimensionality');
43
end
44
45
aa=sum(a.*a,1); bb=sum(b.*b,1); ab=a'*b; 
46
d = sqrt(abs(repmat(aa',[1 size(bb,2)]) + repmat(bb,[size(aa,2) 1]) - 2*ab));