"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 forloops. 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 forloop method might well be the fastest even though it occupies more lines of text. You should try timing them.
Roger Stafford
