how to calculate the distances between pairwise coordinates using loops? Please help me...
Show older comments
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
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
Image Analyst
on 23 Feb 2018
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
John BG
on 25 Feb 2018
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
Sanaz Kb
on 25 Feb 2018
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
Stephen23
on 25 Feb 2018
@Sanaz Kb: please give a link to the new question.
Sanaz Kb
on 25 Feb 2018
https://nl.mathworks.com/matlabcentral/answers/384700-how-to-calculate-the-distances-between-points-coordinates-and-the-arbitrary-reference-point-using-lo
Categories
Find more on Elementary Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!