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);
pn = rand(1000000,2);
tic
d = sqrt(sum((p1-pn).^2,2));
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.
5 Comments
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729376
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729376
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729429
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729429
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729453
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729453
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729496
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_729496
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_731574
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/473890-fast-2d-distance-calculation#comment_731574
Sign in to comment.