フィルターのクリア

How to speed up a euclidean distance calculation between one pixel and many others

2 ビュー (過去 30 日間)
Stephanie Diaz
Stephanie Diaz 2018 年 11 月 14 日
コメント済み: Guillaume 2018 年 11 月 14 日
I am calculating the euclidean distance between a pixel (x,y) and many other pixels in a matrix. I identify the x and y coordinates of the pixels beforehand, and save their x and y values in two column vectors x2 and y2, respectively I currently use the following:
distance = (sqrt((x2-x).^2 + (y2-y).^2))
This works, but is very slow. How can I speed this up?
  1 件のコメント
Guillaume
Guillaume 2018 年 11 月 14 日
This works, but is very slow.
How did you establish that it is slow?
>> coord = rand(1e6, 2);
>> tic; distance = sqrt((coord(:, 1) - coord(1, 1)).^2 + (coord(:, 2) - coord(1, 2)).^2); toc
Elapsed time is 0.029055 seconds.
As you can see it takes around 30 milliseconds on my computer to process a million coordinates.
Most likely, it's another part of your program that is slow, so show us your code.
Note that using hypot would be safer (albeit neglibly slower) than doing the calculation yourself:
distance = hypot(x2-x, y2-y);

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

回答 (1 件)

madhan ravi
madhan ravi 2018 年 11 月 14 日
use norm()
  1 件のコメント
Stephanie Diaz
Stephanie Diaz 2018 年 11 月 14 日
OK, but how do i apply this to more than one pixel? Since "norm(b-a)." only gives me one value

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by