Asked by Neuropragmatist
on 29 Jul 2019

Hi all,

Many of the codes I am currently using depend on a simple calculation: the distance between a single point and a set of other points.

In one example, using the matlab profiler I see that this single calculation takes 50% of the total function time, so I would like to optimise it as far as possible.

I have looked around and haven't found anything more optimal than:

p1 = rand(1,2); % single point

pn = rand(1000000,2); % random points

tic

d = sqrt(sum((p1-pn).^2,2)); % calculate the distance between these

toc

Does anyone else have a clever idea that would optimise this - even just by a tiny fraction? Is there any way to speed these calculations up on the GPU or using a mex? I would be really happy to see any suggestions.

I suspect this might be already be as mathematically simple as possible, but I'm frustrated because I need to calculate this a lot.

I have already vectrorised my code as far as possible.

Thanks for any help,

R.

Answer by Matt J
on 29 Jul 2019

Edited by Matt J
on 29 Jul 2019

If you have the Parallel Computing Toolbox, you can execute the computations on the GPU just by building p1 and pn as gpuArrays. That should definitely speed things up.

gd=gpuDevice;

p1 = gpuArray.rand(1,2);

pn = gpuArray.rand(1000000,2);

tic

d = sqrt(sum((p1-pn).^2,2));

wait(gd);

toc %Elapsed time is 0.001429 seconds.

Neuropragmatist
on 29 Jul 2019

Matt J
on 29 Jul 2019

Sign in to comment.

Answer by Joss Knight
on 3 Aug 2019

pdist2 is the usual way to do this, if you have Statistics and Machine Learning Toolbox.

Neuropragmatist
on 3 Aug 2019

From what I have read, computing the distance directly using the equation is faster than using pdist or pdist2 as it avoids the overheads involved with calling the function.

I don't know if this remains true if you are computing pairwise distances, but I only need to know the distance from one point to many other points.

Joss Knight
on 3 Aug 2019

But pdist2 does that. Input x is a 1-by-2 vector, and input y is an N-by-2 array of N points.

You may be right that it is no faster than implementing it manually.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Adam (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729376

## Neuropragmatist (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729429

## Adam (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729453

## Neuropragmatist (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729496

## Neuropragmatist (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_731574

Sign in to comment.