Calculating efficiently the euclidean distance

1 回表示 (過去 30 日間)
Riccardo Tronconi
Riccardo Tronconi 2021 年 7 月 19 日
コメント済み: Riccardo Tronconi 2021 年 7 月 19 日
Hi guys I would like to calculate the euclidean distance without a for loop to speed up my running time.
I have a timetable A as input with m-rows and 4 columns:
1st column: X point 1
2nd column: Y point 1
3rd column: X point 2
4th column: Y point2
Now I loop over each row of the timetable and I use norm( A(i, 1:2) - A(i, 3:4)). Of course the output should be mx1.
Is there a more efficient way to compute this operation?

採用された回答

Matt J
Matt J 2021 年 7 月 19 日
編集済み: Matt J 2021 年 7 月 19 日
In recent Matlab, you can just do,
vecnorm(A(:, 1:2) - A(:, 3:4),2,2);
In less recent Matlab, you can do,
sqrt(sum( (A(:, 1:2) - A(:, 3:4)).^2,2))
  1 件のコメント
Riccardo Tronconi
Riccardo Tronconi 2021 年 7 月 19 日
thanks a lot!!! :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by