Faster way to calculate pairwise distance?

11 views (last 30 days)
Shubham
Shubham on 1 Mar 2013
I have a matrix p x n, with row vector as coordinate in space. I need to calculate distance between all pairs of these coordinates. Since my p is very large(~50,000), I want to write the code without using any for loop and using only built-in function. I have reduced the code till 1 for loop, still it takes lot of time. I have only signal processing toolbox available with me.
My code looks like this:
if true
Y = a + rand(50000,5)*(b-a);
for i=1:p-1
j=i+1:p
D(j,i) = sqrt(sum(bsxfun(@minus,Y(i,:),Y(j,:)).^2,2));
end
end
Any help would be much appreciated.
  1 Comment
Matt Kindig
Matt Kindig on 1 Mar 2013
Well the first thing you could do is pre-allocate D, by inserting this line prior to the for loop:
D = zeros(p-1,p);
However, Shashank is right--you will run into major issues with memory soon.

Sign in to comment.

Answers (1)

Shashank Prasanna
Shashank Prasanna on 1 Mar 2013
pdist
But part of the statistics toolbox.
Alternatively you could generate all pairwise combinations of row using
nchoosek
And then calculate distance as above. But you may hit the memory wall, as the nchoosek output will be huge.

Community Treasure Hunt

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

Start Hunting!