Thread Subject: euclidean distance

Subject: euclidean distance

From: kudrah

Date: 30 Jun, 2009 19:31:01

Message: 1 of 5

Hi everyone!
Im trying to calculate the euclidean distance between 2 matrices that contain 3d points.The matrices are quite large,eg A=19500x3 and B=2000x3.I want to calculate the distance between each point of A to each point of B and thus produce a 19500x2000 matrix.So far i wrote this but it takes like forever..Is there any faster way???

for i=1:size(A,1);
    for k=1:size(B,1);
Eucl_Ideal(i,k)=norm(A(i,:)-B(k,:));
    end
end

Thank you in advance!!!!

Subject: euclidean distance

From: Stefan Stefan

Date: 30 Jun, 2009 21:25:03

Message: 2 of 5


First I would advise to preallocate the resulting matrix like

Eucl_Ideal=zeros(size(A,1),size(B,1));

in advance to the for loop.

Regards,
Stefan


"kudrah " <elzarogia@yahoo.gr> wrote in message <h2dp5l$p98$1@fred.mathworks.com>...
> Hi everyone!
> Im trying to calculate the euclidean distance between 2 matrices that contain 3d points.The matrices are quite large,eg A=19500x3 and B=2000x3.I want to calculate the distance between each point of A to each point of B and thus produce a 19500x2000 matrix.So far i wrote this but it takes like forever..Is there any faster way???
>
> for i=1:size(A,1);
> for k=1:size(B,1);
> Eucl_Ideal(i,k)=norm(A(i,:)-B(k,:));
> end
> end
>
> Thank you in advance!!!!

Subject: euclidean distance

From: John D'Errico

Date: 30 Jun, 2009 22:53:01

Message: 3 of 5

"kudrah " <elzarogia@yahoo.gr> wrote in message <h2dp5l$p98$1@fred.mathworks.com>...
> Hi everyone!
> Im trying to calculate the euclidean distance between 2 matrices that contain 3d points.The matrices are quite large,eg A=19500x3 and B=2000x3.I want to calculate the distance between each point of A to each point of B and thus produce a 19500x2000 matrix.So far i wrote this but it takes like forever..Is there any faster way???
>
> for i=1:size(A,1);
> for k=1:size(B,1);
> Eucl_Ideal(i,k)=norm(A(i,:)-B(k,:));
> end
> end
>
> Thank you in advance!!!!

This matrix will take roughly 300 megabytes to store.
This is one reason why it takes forever. A bigger reason
for the slowness is that you failed to preallocate the
memory.

Finally, you might convert to single precision to store
the distances, so this might work best:

D = bsxfun(@times,single(A(:,1)),single(B(:,1)).');
D = D + bsxfun(@times,single(A(:,2)),single(B(:,2)).');
D = D + bsxfun(@times,single(A(:,3)),single(B(:,3)).');
D = sqrt(D);

HTH,
John

Subject: euclidean distance

From: Peter Perkins

Date: 1 Jul, 2009 14:25:26

Message: 4 of 5

kudrah wrote:
> Hi everyone!
> Im trying to calculate the euclidean distance between 2 matrices that contain 3d points.The matrices are quite large,eg A=19500x3 and B=2000x3.I want to calculate the distance between each point of A to each point of B and thus produce a 19500x2000 matrix.So far i wrote this but it takes like forever..Is there any faster way???
>
> for i=1:size(A,1);
> for k=1:size(B,1);
> Eucl_Ideal(i,k)=norm(A(i,:)-B(k,:));
> end
> end

As others have said, preallocate the result. Also:

Because of the way data is stored in MATLAB, your loops are in the wrong order -- you want to have the row index in Eucl_Ideal varying fastest or you will be accessing memory very non-sequentially.

But most likely, you want to loop over 1:3 within 1:2000, and sum the squared diffs for each dimension of each point in B to all points in A, vectorized. This is essentially what John suggested with BSXFUN (though I think those ought to be @minus's, wrapped in .^2's). Here's an "unrolled loop" version.

Eucl_Ideal = zeros(size(A,1),size(B,1));
for i = 1:size(B,1);
    Eucl_Ideal(:,i) = sqrt((A(:,1)-B(i,1)).^2 + (A(:,2)-B(i,2)).^2 + (A(:,3)-B(i,3)).^2);
end

There's a good deal that can be learned about memory access and vectorization from this example. Hope this helps.

Subject: euclidean distance

From: oruganti murthy

Date: 2 Jul, 2009 03:24:01

Message: 5 of 5

Hi peter,
Thank you very much.
Your solution was very helpful to me in my situation (except that I have 150 dimensions instead of 3 !).
In my situation, I just need the index (of point) of B which is nearest to each point in B. Instead of finding the pairwise distance and using min command, can I bypass this laborious calculation of calculating Euclidean distance to directly arrive at the indices.


regards,
ramana
 
> But most likely, you want to loop over 1:3 within 1:2000, and sum the squared diffs for each dimension of each point in B to all points in A, vectorized. This is essentially what John suggested with BSXFUN (though I think those ought to be @minus's, wrapped in .^2's). Here's an "unrolled loop" version.
>
> Eucl_Ideal = zeros(size(A,1),size(B,1));
> for i = 1:size(B,1);
> Eucl_Ideal(:,i) = sqrt((A(:,1)-B(i,1)).^2 + (A(:,2)-B(i,2)).^2 + (A(:,3)-B(i,3)).^2);
> end
>
> There's a good deal that can be learned about memory access and vectorization from this example. Hope this helps.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
euclidian distance Sprinceana 1 Jul, 2009 11:13:51
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com