Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Calculate distance between points

Subject: Calculate distance between points

From: Tan Has

Date: 25 Jan, 2012 20:45:10

Message: 1 of 4

Hi I am very new in matlab. Can any body help me to figure it out?

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.

Example data:
(X1, Y1) (x1, y1)
(X2, Y2) (x2, y2)
(X3, Y3) (x3, y3)
(..., ....) (..., ....)
(..., ....) (..., ....)
(..., ....) (..., ....)
(Xn, Yn) (xm, ym)

for I = 1, n
for I = 1, m
dist=sqrt((X(I)-x(I))^2+(Y(I)-Y(I))^2)
end
end

IS THIS THE CODE???

Thanks a lot.

Subject: Calculate distance between points

From: TideMan

Date: 25 Jan, 2012 21:26:29

Message: 2 of 4

On Jan 26, 9:45 am, "Tan Has" <tanzeerha...@gmail.com> wrote:
> Hi I am very new in matlab. Can any body help me to figure it out?
>
> 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.
>
> Example data:
> (X1, Y1)  (x1, y1)
> (X2, Y2)  (x2, y2)
> (X3, Y3)  (x3, y3)
> (..., ....)  (..., ....)
> (..., ....)  (..., ....)
> (..., ....)  (..., ....)
> (Xn, Yn)  (xm, ym)
>
> for I = 1, n
> for I = 1, m
> dist=sqrt((X(I)-x(I))^2+(Y(I)-Y(I))^2)
> end
> end
>
> IS THIS THE CODE???
>
> Thanks a lot.

No it won't work.
But I cannot figure out what your matrix is.
You gave us example data, but it makes no sense in Matlab parlance.
You also said the matrix was 10x20, but where are the 20 columns? You
only showed 4.

Subject: Calculate distance between points

From: ImageAnalyst

Date: 26 Jan, 2012 02:25:07

Message: 3 of 4

I also don't know what the 10 by 20 refers to. "I" is also a bad
variable to use - on my computer it looks like a one so that Y(I)
looks just like Y(1) so that the difference looks like it would be
zero. Anyway do you perhaps want to do this:
for column = 1 : n
  for row = 1 : m
    dist(row, column) = sqrt(row^2+column^2);
  end
end

Or maybe X and Y are long 1D lists of coordinates, in which case you
might want to do this:
for k = 1 : length(X)
  dist(k) = sqrt( (X(k) - X(1))^2 + (Y(k) - Y(1))^2);
end
Where you're finding the distance from each (x,y) coordinate pair to
the first coordinate pair (X(1), Y(1)).

Subject: Calculate distance between points

From: Roger Stafford

Date: 26 Jan, 2012 03:36:09

Message: 4 of 4

"Tan Has" wrote in message <jfppkm$ke1$1@newscl01ah.mathworks.com>...
> 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.
- - - - - - - - -
  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 for-loops. Is that correct? (Your explanation is somewhat lacking in clarity.)

  Let A be the name of the first matrix and B the name of the second.

 D = zeros(n,m);
 for I = 1:n
  for J = 1:m
   D(I,J) = sqrt( (A(I,1)-B(J,1))^2 + (A(I,2)-B(J,2))^2 );
  end
 end

The distances between pairs of points are given in the n by m matrix, D.

  Alternate methods are:

 D = sqrt((bsxfun(@minus,A(:,1),B(:,1).')).^2+...
          (bsxfun(@minus,A(:,2),B(:,2).')).^2);

or

 [I,J] = ndgrid(1:n,1:m);
 D = reshape(sqrt((A(I,1)-B(J,1)).^2+(A(I,2)-B(J,2)).^2),n,m);

However, the for-loop method might well be the fastest even though it occupies more lines of text. You should try timing them.

Roger Stafford

Tags for this Thread

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.

Contact us