Info

This question is closed. Reopen it to edit or answer.

FInd distance between surface coordinates without repetition

1 view (last 30 days)
Hi
Suppose, I have list of 4 coordinates -
x[ -32.8778 -10.8573 11.3161 33.4895]
Y[-32.8778 -10.8573 11.3161 33.4895]
i want to calculate distance between all possible combinations (x and y) without repetition, in a for loop (as i need to perform this with larger dataset).
and getting a distance matrix along with coresponding x, y position.
could you please help me?
thanks in advance..!

Answers (2)

Marco Riani
Marco Riani on 12 May 2020
If x and y are
x=[ -32.8778 -10.8573 11.3161 33.4895];
y=[ -32.8778 -10.8573 11.3161 33.4895];
the distance matrix is
abs(x'-y)
0 22.0205 44.1939 66.3673
22.0205 0 22.1734 44.3468
44.1939 22.1734 0 22.1734
66.3673 44.3468 22.1734 0
or I am missing something of your question?
  1 Comment
Abhilash Uniyal
Abhilash Uniyal on 13 May 2020
ok let me put this way.
I have 4 different coordinates.
ex point 1 =x(1),y(1)
from which i have saparated each x position and y position.
x=[x1 x2 x3 x4]
y=[y1 y2 y3 y4]
distance from point 1 to 2-
point 1 to 2=sqrt(y(1)-y(2))^2+(x(1)-x(2))^2);

Marco Riani
Marco Riani on 13 May 2020
Thanks for the clarification. Please let me know if the code below is what you wanted
% Define x and y
x=1:4;
y=2:5;
lx=length(x);
D=zeros(lx,lx);
for i=1:lx
for j=1:lx
D(i,j)=sqrt((x(i)-x(j))^2+(y(i)-y(j))^2);
end
end

This question is closed.

Community Treasure Hunt

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

Start Hunting!