MATLAB Answers

how to find out distance between two points.

4 views (last 30 days)
Fuzail Shad
Fuzail Shad on 11 Feb 2020
Commented: Luna on 19 Feb 2020
I have 8*2 matrix of numbers like
2 1
3 2
4 3
5 2
6 2
7 3
8 3
9 8
and another matrix of coordinates for these numbers like
1 19.75303200 0.43557600 2.65388300
2 0.77047300 0.06860900 2.48736300
3 1.50647300 19.88576300 3.80387500
4 2.93388400 19.40857700 3.52875700
5 1.29467400 0.79392000 1.85730000
6 0.70911200 19.12320300 1.93804200
7 1.57899700 0.85145000 4.31768200
8 0.80276600 18.88574500 4.70559200
9 0.74693100 17.90090500 4.22987600
now I have to find out the distace between points from column 1 to 2 of first marix using the coordinates from 2nd matrix. how can i do it?

  1 Comment

Luna
Luna on 19 Feb 2020
if it is a solution please accept my answer below :)

Sign in to comment.

Answers (1)

Luna
Luna on 11 Feb 2020
Edited: Luna on 11 Feb 2020
Here you can use this:
NodeNumbersMatrix = [...
2 1
3 2
4 3
5 2
6 2
7 3
8 3
9 8 ...
];
CoordinatesMatrix = [...
1 19.75303200 0.43557600 2.65388300
2 0.77047300 0.06860900 2.48736300
3 1.50647300 19.88576300 3.80387500
4 2.93388400 19.40857700 3.52875700
5 1.29467400 0.79392000 1.85730000
6 0.70911200 19.12320300 1.93804200
7 1.57899700 0.85145000 4.31768200
8 0.80276600 18.88574500 4.70559200
9 0.74693100 17.90090500 4.2298760 ...
];
distance_array = nan(size(NodeNumbersMatrix,1),1); % preallocate
for i = 1:size(NodeNumbersMatrix,1)
points_to_look_between = NodeNumbersMatrix(i,:);
two_vectors_of_points = CoordinatesMatrix(points_to_look_between,2:end);
distance_of_two_vectors = sqrt(sum((two_vectors_of_points(1,:) - two_vectors_of_points(2,:)).^2));
distance_array(i) = distance_of_two_vectors;
end
%% OR
% shorter way for loop
for i = 1:size(NodeNumbersMatrix,1)
two_vectors_of_points = CoordinatesMatrix( NodeNumbersMatrix(i,:),2:end);
distance_array(i) = sqrt(sum((two_vectors_of_points(1,:) - two_vectors_of_points(2,:)).^2));
end

  0 Comments

Sign in to comment.

Sign in to answer this question.