compact way to calculate euclidean distance among points

1 回表示 (過去 30 日間)
Sim
Sim 2022 年 10 月 11 日
編集済み: Sim 2022 年 10 月 12 日
What is a compact way to calculate the euclidean distance among point "a" and the four points "b" ?
% Input:
a = [0,0]; % x_a, y_a
b = [0,1; % x_b, y_b
2,1;
1,1;
1,3];
% Output:
% A non-compact solution, including the visualization of points and distances (lines)
hold on
plot(a(:,1),a(:,2),'o','markerfacecolor','r') % point "a"
plot(b(:,1),b(:,2),'o','markerfacecolor','r') % points "b"
for i = 1 : 4
d(i,:) = sqrt( (a(:,1)-b(i,1))^2 + (a(:,2)-b(i,2))^2 ); % euclidean distance
plot([a(:,1), b(i,1)], [a(:,2), b(i,2)]) % lines connecting point "a" to points "b"
end
hold off
axis equal
d
d = 4×1
1.0000 2.2361 1.4142 3.1623

採用された回答

Torsten
Torsten 2022 年 10 月 11 日
% x_a, y_a
a = [0,0];
% x_b, y_b
b = [0,1;
2,1;
1,1;
1,3];
D = pdist2(a,b)
D = 1×4
1.0000 2.2361 1.4142 3.1623

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by