how to find out distance between two points.
조회 수: 1 (최근 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
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!