<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255025</link>
    <title>MATLAB Central Newsreader - euclidean distance</title>
    <description>Feed for thread: euclidean distance</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 30 Jun 2009 19:31:01 -0400</pubDate>
      <title>euclidean distance</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255025#661628</link>
      <author>kudrah </author>
      <description>Hi everyone!&lt;br&gt;
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???&lt;br&gt;
&lt;br&gt;
for i=1:size(A,1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for k=1:size(B,1);&lt;br&gt;
Eucl_Ideal(i,k)=norm(A(i,:)-B(k,:));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end &lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Thank you in advance!!!!</description>
    </item>
    <item>
      <pubDate>Tue, 30 Jun 2009 21:25:03 -0400</pubDate>
      <title>Re: euclidean distance</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255025#661663</link>
      <author>Stefan Stefan</author>
      <description>&lt;br&gt;
First I would advise to preallocate the resulting matrix like&lt;br&gt;
&lt;br&gt;
Eucl_Ideal=zeros(size(A,1),size(B,1));&lt;br&gt;
&lt;br&gt;
in advance to the for loop.&lt;br&gt;
&lt;br&gt;
Regards,&lt;br&gt;
Stefan&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;kudrah &quot; &amp;lt;elzarogia@yahoo.gr&amp;gt; wrote in message &amp;lt;h2dp5l$p98$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi everyone!&lt;br&gt;
&amp;gt; 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???&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i=1:size(A,1);&lt;br&gt;
&amp;gt;     for k=1:size(B,1);&lt;br&gt;
&amp;gt; Eucl_Ideal(i,k)=norm(A(i,:)-B(k,:));&lt;br&gt;
&amp;gt;     end &lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you in advance!!!!</description>
    </item>
    <item>
      <pubDate>Tue, 30 Jun 2009 22:53:01 -0400</pubDate>
      <title>Re: euclidean distance</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255025#661684</link>
      <author>John D'Errico</author>
      <description>&quot;kudrah &quot; &amp;lt;elzarogia@yahoo.gr&amp;gt; wrote in message &amp;lt;h2dp5l$p98$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi everyone!&lt;br&gt;
&amp;gt; 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???&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i=1:size(A,1);&lt;br&gt;
&amp;gt;     for k=1:size(B,1);&lt;br&gt;
&amp;gt; Eucl_Ideal(i,k)=norm(A(i,:)-B(k,:));&lt;br&gt;
&amp;gt;     end &lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you in advance!!!!&lt;br&gt;
&lt;br&gt;
This matrix will take roughly 300 megabytes to store.&lt;br&gt;
This is one reason why it takes forever. A bigger reason&lt;br&gt;
for the slowness is that you failed to preallocate the&lt;br&gt;
memory.&lt;br&gt;
&lt;br&gt;
Finally, you might convert to single precision to store&lt;br&gt;
the distances, so this might work best:&lt;br&gt;
&lt;br&gt;
D = bsxfun(@times,single(A(:,1)),single(B(:,1)).');&lt;br&gt;
D = D + bsxfun(@times,single(A(:,2)),single(B(:,2)).');&lt;br&gt;
D = D + bsxfun(@times,single(A(:,3)),single(B(:,3)).');&lt;br&gt;
D = sqrt(D);&lt;br&gt;
&lt;br&gt;
HTH,&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Wed, 01 Jul 2009 14:25:26 -0400</pubDate>
      <title>Re: euclidean distance</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255025#661844</link>
      <author>Peter Perkins</author>
      <description>kudrah wrote:&lt;br&gt;
&amp;gt; Hi everyone!&lt;br&gt;
&amp;gt; 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???&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for i=1:size(A,1);&lt;br&gt;
&amp;gt;     for k=1:size(B,1);&lt;br&gt;
&amp;gt; Eucl_Ideal(i,k)=norm(A(i,:)-B(k,:));&lt;br&gt;
&amp;gt;     end &lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
As others have said, preallocate the result.  Also:&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
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 &quot;unrolled loop&quot; version.&lt;br&gt;
&lt;br&gt;
Eucl_Ideal = zeros(size(A,1),size(B,1));&lt;br&gt;
for i = 1:size(B,1);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Eucl_Ideal(:,i) = sqrt((A(:,1)-B(i,1)).^2 + (A(:,2)-B(i,2)).^2 + (A(:,3)-B(i,3)).^2);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
There's a good deal that can be learned about memory access and vectorization from this example.  Hope this helps.</description>
    </item>
    <item>
      <pubDate>Thu, 02 Jul 2009 03:24:01 -0400</pubDate>
      <title>Re: euclidean distance</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/255025#662086</link>
      <author>oruganti murthy</author>
      <description>Hi peter,&lt;br&gt;
Thank you very much.&lt;br&gt;
Your solution was very helpful to me in my situation (except that I have 150 dimensions instead of 3 !). &lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
regards,&lt;br&gt;
ramana&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; 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 &quot;unrolled loop&quot; version.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Eucl_Ideal = zeros(size(A,1),size(B,1));&lt;br&gt;
&amp;gt; for i = 1:size(B,1);&lt;br&gt;
&amp;gt;     Eucl_Ideal(:,i) = sqrt((A(:,1)-B(i,1)).^2 + (A(:,2)-B(i,2)).^2 + (A(:,3)-B(i,3)).^2);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; There's a good deal that can be learned about memory access and vectorization from this example.  Hope this helps.</description>
    </item>
  </channel>
</rss>

