how to find out distance between two points.

3 次查看(过去 30 天)
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 个)

Luna
Luna 2020-2-11
编辑:Luna 2020-2-11
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

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by