how to calculate the distances between pairwise coordinates using loops? Please help me...

Hi,
I have a 4x6 matrix(A).
A= [50 56 76 55 63 63;
51 60 77 55 63 62;
51 60 77 55 64 64;
51 59 75 55 62 63]
Matrix A was formed as:
A=[X1 Y1 X2 Y2 X3 Y3]
or A=[point1 point2 point3] % point1 is (X1,Y1), point2(X2,Y2),
first column is keeping the x-coordinate...etc.
I have 4 observations(rows) and each observation has 3 points(6 columns). I want to calculate the eucledian distance and create a new matrix which is holding the all possible distances for each observation.
for example; distances for observation 1:
distance1=sqrt((X1-X2)^2 + (Y1-Y2)^2), %distance between point1 and point2
distance2=sqrt((X1-X3)^2 + (Y1-Y3)^2), %distance between point1 and point3
distance3=sqrt((X2-X1)^2 + (Y2-Y1)^2), %distance between point2 and point1
distance4=sqrt((X2-X3)^2 + (Y2-Y3)^2), %distance between point2 and point3
distance5=sqrt((X3-X1)^2 + (Y3-Y1)^2), %distance between point3 and point1
distance6=sqrt((X3-X2)^2 + (Y3-Y2)^2), %distance between point3 and point2 and
also repetitive distance is not important
I want to use any loop to creat my distances matrix which is shown below
distancesMatrix=[distance1 distance2 distance3 distance4 distance5 distance6]; % 4x6 matrix
could you please help me?
thanks in advance..

 Accepted Answer

Yavuz
In this question you have only 3 points, but you may need more.
The function combinator.m is really useful when the amount of points increases, copy of combinator.m following these lines.
You mention in your question that you need the combinations with repetition. With 3 points makes 6:
combinator(3,2,'p')
=
2 1
1 2
3 1
1 3
3 2
2 3
However, since you are calculating distances you don't really need to calculate 6 distances, but just 3, because distance 1-2 is same as 2-1 and so on, then you need:
combinator(3,2,'c')
=
1 2
1 3
2 3
The answer starts here:
amount_points=3
amount_dist=numel(combinator(amount_points,2,'c'))/2 % how many distances to calculate for each round
[szA1 szA2]=size(A)
amount_observations=szA1 % how many rounds
distance=zeros(amount_observations,amount_dist) % make room for result
pair=combinator(amount_dist,2,'c') % all point pairs
for k=1:amount_observations
for j=1:amount_dist
distance(k,j)=(A(k,pair(j,1)).^2+A(k,pair(j,2)).^2).^.5
end
end
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John
EDIT: copyright code removed

1 Comment

Hi John,
İt is really helpfull. Thanks a lot.
I solved my problem thanks to your help.

Sign in to comment.

More Answers (1)

Hello,
I have the same question. but instead of relating one-by-one points,I should relate all points with one reference point. could you pleas help me?
Another question: i implement your function code in my Matlab but it doesn't work. (It get me an error message in this line- error('Only 2, 3 or 4 inputs are allowed. See help.')-
Thanks

6 Comments

If you have vectors x and y, and a reference point (xRef, yRef) then you can compute the distance of all points in x and y to the reference point using sqrt():
distances = sqrt((x - xRef) .^ 2 + (y - yRef) .^ 2);
Hello,
Many thanks for your answer. in my question the X and Y have points inside which are increase sequentially. For example:
Let consider : X = [0 2 4 6; 0 2 4 6; 0 2 4 6]; and Y = [0 0 0 0;2 2 2 2;4 4 4 4;6 6 6 6]; all of the points inside X and Y are due to the geometry of my model configuration. Let consider a reference point which is inside the model. I need to find the distance between all points and reference point. I know that I should use a loop for the sequential changes but I don't know how?
I should note that the configuration may be changed.
Thanks Sanaz
Hi Sanaz
it would greatly help if you start a new question with the new query
I have the same question. but instead of relating one-by-one points,I should relate all points with one reference point. could you pleas help me?
It's understandable that because it's a variant of this already solved question, it's easier to just post along the new question along the already solved one, but it would be really appreciated if a new question is posted, no need to repeat things, just paste a link to this question
and then add the variation
I have the same question. but instead of relating one-by-one points,I should relate all points with one reference point. could you pleas help me?
right?
thanks for time and attention
John BG
Thank you John, I do the same thing that you announced here. I have created a new question within this tag "euclidean distances" and also I have copied the link.
kind regards Sanaz
@Sanaz Kb: please give a link to the new question.
https://nl.mathworks.com/matlabcentral/answers/384700-how-to-calculate-the-distances-between-points-coordinates-and-the-arbitrary-reference-point-using-lo

Sign in to comment.

Categories

Find more on Elementary Math in Help Center and File Exchange

Asked:

on 1 Apr 2016

Commented:

on 25 Feb 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!