How can I use the GPU to parallelize computation of average pairwise distances?

3 views (last 30 days)
I have a=rand(1, 1000) and b=rand(1, 1000), and I want to compute the average pairwise Euclidean distance between every point in A and every point in B. I don't need any of the individual distances.
I was able to get the Mandelbrot set GPU example running successfully on my computer, which walks you through either 1) just allocating arrays with gpuArray() and doing your computations, or 2) parallelizing with arrayfun() for much better performance.
However, I don't see a good way of shoehorning my problem into the arrayfun() requirements. One possibility might be to just form the matrix [a' repmat(b, 1000, 1)], which has a point of A in the first column of each row, and then all of the points of B in the rest of each row, and then if arrayfun() could just pass rows of this matrix as individual 'elements', that could work. But it wasn't clear to me if arrayfun() is capable of that.
Any thoughts?
Thanks.

Accepted Answer

Joss Knight
Joss Knight on 21 Jul 2018
You can do it with basic arithmetic but really you're just looking for the function pdist2.

More Answers (1)

Joss Knight
Joss Knight on 24 Jul 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!