Fast 2D distance calculation
이전 댓글 표시
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.
댓글 수: 5
Do you always need the exact distance or just the relative distances? e.g. if you only need to find, for example, which point is closest you can omit the expensive sqrt operation because ordering is unchanged by this. This is used in many contexts as a way to speed up distance-based calculations where the exact distance itself is not actually needed.
You can test out if using the GPU speeds it up very quickly. Just put it in a GPU array and try it. If you have Matlab Coder you can likewise try a C++ or C equivalent very quickly (or just program your own as it is a trivial function) and test the speed difference.
Neuropragmatist
2019년 7월 29일
Adam
2019년 7월 29일
How long is this taking you exactly? When I ran it with the profiler on (i.e. in normal usage it should be faster) the distance calculation took 0.027s on your given example.
Neuropragmatist
2019년 7월 29일
Neuropragmatist
2019년 8월 3일
답변 (2개)
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.
댓글 수: 2
Neuropragmatist
2019년 7월 29일
Matt J
2019년 7월 29일
Well, I don't think the question can be taken any further until we know what parallel computing resources you do have, or can remote connect to. I think you are at the limits of performance already with standard Matlab.
Joss Knight
2019년 8월 3일
0 개 추천
pdist2 is the usual way to do this, if you have Statistics and Machine Learning Toolbox.
댓글 수: 2
Neuropragmatist
2019년 8월 3일
Joss Knight
2019년 8월 3일
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.
카테고리
도움말 센터 및 File Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!