File Exchange

## distance.m

version 1.0 (1.28 KB) by

A fully vectorized function that computes the Euclidean distance matrix between two sets of vectors.

Updated

A fully vectorized function that computes the Euclidean distance matrix between two sets of vectors.

The output is the same as MathWorks' (Neural Network Toolbox) 'dist' funtion (ie, d = dist(A',B), where A is a (DxM) matrix and B a (DxN) matrix, returns the same as my d = distance(A,B) ), but this function executes much faster.

ashkan abbasi

Patrick Eimsing

Maksim

### Maksim (view profile)

Faster than builtin pdist2 in R2016a.

Biaobin Jiang

Benoit Thouy

### Benoit Thouy (view profile)

Following the analysis of Stuart Layton, I've found a merely faster way to perform the same computation:

d = sqrt(bsxfun(@minus,bsxfun(@plus,sum(a.^2,1),sum(b.^2,1)'),2*(a'*b).'));

Using the same tool as Stuart Layton (modified to have the tic/toc outside the loop), I measure 4.3607ms for distance.m and 4.1252ms for mine. Does not change dramatically, ok.

Zeeshan

### Zeeshan (view profile)

Hi, I am new to this so how would I exactly inputthis on matlab? if anyone can help me please

Arun Kumar Chithanar

### Arun Kumar Chithanar (view profile)

Thanks. This saved me time!

vince

### vince (view profile)

I find a simple

sqrt((a-b)'*(a-b))

faster to compute the Euclidean distance

Ali Asghar Torabi

### Ali Asghar Torabi (view profile)

Thanks. using repmat has made it useful and fast.

Andrea Tacchetti

### Andrea Tacchetti (view profile)

Things can go wrong in extreme cases.
If dimensionality is very high numerical errors will kick in.

I have twenty-five hundred 10'000 dimensional vectors stored in matrix X and

~
sum(diag(distance(X',X'))) = 6e-4;
~

Other than that, great job!

T. R.

George

### George (view profile)

Why is the -2ab term included?

Stuart Layton

### Stuart Layton (view profile)

I find the original code to be faster than the method put forth by oliver
~~~~~~
s = 100000; a = rand(5,s); b = rand(5,1); t = []; n = 1000;
disp(['Data Points: ', num2str(s), ' iterations: ', num2str(n)]);
for i=1:n
tic; d = distance(a,b); t(i) = toc;
end

disp(['Average run time: ', num2str(mean(t))]);
t = [];
for i=1:n
tic; d = sqrt(bsxfun(@plus,dot(a,a,1)',dot(b,b,1))-2*a'*b); t(i) = toc;
end
disp(['Average run time: ', num2str(mean(t))]);
~~~~~~

Data Points: 100000 iterations: 1000
Average run time: 0.01356
Average run time: 0.020044

fabio freschi

### fabio freschi (view profile)

According to Oliver Woodford, on my macbook the fastest is

d = sqrt(bsxfun(@plus,dot(a,a,1)',dot(b,b,1))-2*a'*b);

Oliver Woodford

### Oliver Woodford (view profile)

Could be faster still by using bsxfun:
d = sqrt(abs(bsxfun(@plus, aa', bb) - 2*a'*b));

Philipp Berens

Quick and clean.

What might be nice: Optional only one argument.

Panyam Narahari Sastry

Good

M. Shoaib Sehgal

for vectors it simple to compute using:

distance = norm(A-B)

A. S.

Very useful, especially Markus' version.

Xi-Lin Li

very good code

Thanks

Markus Buehren

Sorry, I forgot the sqrt:

d = sqrt(abs(aa( ones(size(bb,2),1), :)' + bb( ones(size(aa,2),1), :) - 2*a'*b));

Markus Buehren

Simple but nice. Performance can still be improved by about 60% by avoiding calls to repmat:

d = abs(aa( ones(size(bb,2),1), :)' + bb( ones(size(aa,2),1), :) - 2*a'*b);

Markus

Enrique Guevara

Is a very fast code. Is faster than any other code for any vectors biger that 8 points.

Catherine Sweeney-Reed

Great! Thanks!

Babak Taati

good!

Anton Cervantes

Hsuan-Tien Lin

great code

uday desai

Emily Van Ark

great, fast little function. Exactly what I was looking for!

need the file

ti saigon

Peter G

useful...good

Jan de Vries

Fantastic!

MATLAB 5.2 (R10)