http://www.mathworks.com/matlabcentral/newsreader/view_thread/316286
MATLAB Central Newsreader  Calculate distance between points
Feed for thread: Calculate distance between points
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 25 Jan 2012 20:45:10 +0000
Calculate distance between points
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316286#864843
Tan Has
Hi I am very new in matlab. Can any body help me to figure it out?<br>
<br>
Lets, I have a matrix (10 X 20), I want to calculate the distance of each point of the matrix from one certain point (another array of xy point). I want to check the distance of all points in the matrix with respect to xy point (first with respect to 1st xy coordinate then 2nd and to the end.) I want to use the loop system.<br>
<br>
Example data: <br>
(X1, Y1) (x1, y1)<br>
(X2, Y2) (x2, y2)<br>
(X3, Y3) (x3, y3)<br>
(..., ....) (..., ....)<br>
(..., ....) (..., ....)<br>
(..., ....) (..., ....)<br>
(Xn, Yn) (xm, ym)<br>
<br>
for I = 1, n<br>
for I = 1, m<br>
dist=sqrt((X(I)x(I))^2+(Y(I)Y(I))^2)<br>
end<br>
end<br>
<br>
IS THIS THE CODE???<br>
<br>
Thanks a lot.

Wed, 25 Jan 2012 21:26:29 +0000
Re: Calculate distance between points
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316286#864851
TideMan
On Jan 26, 9:45 am, "Tan Has" <tanzeerha...@gmail.com> wrote:<br>
> Hi I am very new in matlab. Can any body help me to figure it out?<br>
><br>
> Lets, I have a matrix (10 X 20), I want to calculate the distance of each point of the matrix from one certain point (another array of xy point). I want to check the distance of all points in the matrix with respect to xy point (first with respect to 1st xy coordinate then 2nd and to the end.) I want to use the loop system.<br>
><br>
> Example data:<br>
> (X1, Y1) (x1, y1)<br>
> (X2, Y2) (x2, y2)<br>
> (X3, Y3) (x3, y3)<br>
> (..., ....) (..., ....)<br>
> (..., ....) (..., ....)<br>
> (..., ....) (..., ....)<br>
> (Xn, Yn) (xm, ym)<br>
><br>
> for I = 1, n<br>
> for I = 1, m<br>
> dist=sqrt((X(I)x(I))^2+(Y(I)Y(I))^2)<br>
> end<br>
> end<br>
><br>
> IS THIS THE CODE???<br>
><br>
> Thanks a lot.<br>
<br>
No it won't work.<br>
But I cannot figure out what your matrix is.<br>
You gave us example data, but it makes no sense in Matlab parlance.<br>
You also said the matrix was 10x20, but where are the 20 columns? You<br>
only showed 4.

Thu, 26 Jan 2012 02:25:07 +0000
Re: Calculate distance between points
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316286#864865
ImageAnalyst
I also don't know what the 10 by 20 refers to. "I" is also a bad<br>
variable to use  on my computer it looks like a one so that Y(I)<br>
looks just like Y(1) so that the difference looks like it would be<br>
zero. Anyway do you perhaps want to do this:<br>
for column = 1 : n<br>
for row = 1 : m<br>
dist(row, column) = sqrt(row^2+column^2);<br>
end<br>
end<br>
<br>
Or maybe X and Y are long 1D lists of coordinates, in which case you<br>
might want to do this:<br>
for k = 1 : length(X)<br>
dist(k) = sqrt( (X(k)  X(1))^2 + (Y(k)  Y(1))^2);<br>
end<br>
Where you're finding the distance from each (x,y) coordinate pair to<br>
the first coordinate pair (X(1), Y(1)).

Thu, 26 Jan 2012 03:36:09 +0000
Re: Calculate distance between points
http://www.mathworks.com/matlabcentral/newsreader/view_thread/316286#864866
Roger Stafford
"Tan Has" wrote in message <jfppkm$ke1$1@newscl01ah.mathworks.com>...<br>
> Lets, I have a matrix (10 X 20), I want to calculate the distance of each point of the matrix from one certain point (another array of xy point). I want to check the distance of all points in the matrix with respect to xy point (first with respect to 1st xy coordinate then 2nd and to the end.) I want to use the loop system.<br>
        <br>
I am guessing you mean that you have two matrices, one n by 2 and the other m by 2, in which each row gives the x and y coordinates of a point, and you want to compute the distance between each point of one of the matrices to each point of the second one. Moreover you wish to do this in terms of forloops. Is that correct? (Your explanation is somewhat lacking in clarity.)<br>
<br>
Let A be the name of the first matrix and B the name of the second.<br>
<br>
D = zeros(n,m);<br>
for I = 1:n<br>
for J = 1:m<br>
D(I,J) = sqrt( (A(I,1)B(J,1))^2 + (A(I,2)B(J,2))^2 );<br>
end<br>
end<br>
<br>
The distances between pairs of points are given in the n by m matrix, D.<br>
<br>
Alternate methods are:<br>
<br>
D = sqrt((bsxfun(@minus,A(:,1),B(:,1).')).^2+...<br>
(bsxfun(@minus,A(:,2),B(:,2).')).^2);<br>
<br>
or<br>
<br>
[I,J] = ndgrid(1:n,1:m);<br>
D = reshape(sqrt((A(I,1)B(J,1)).^2+(A(I,2)B(J,2)).^2),n,m);<br>
<br>
However, the forloop method might well be the fastest even though it occupies more lines of text. You should try timing them.<br>
<br>
Roger Stafford