Locate elements of a vector inside a meshgrid

1 回表示 (過去 30 日間)
Alberto Belvedere
Alberto Belvedere 2020 年 10 月 6 日
コメント済み: Alberto Belvedere 2020 年 10 月 6 日
I'm trying to build a matrix RES, with the same dimensions of X and Y, that has a '1' in the position pointed (with a tollerance tollX and tollY) by the i-th couple contained in V.
[X,Y]=meshgrid(0:1:3,-2:1:2);
V=[2.1 1.2;
0.2 0.7;
3.1 1.9;
1.6 -1];
tollX=0.5;
tollY=0.5;
RES=0;
for i=1:length(V)
RES=(V(i,1)<X+tollX).*(V(i,1)>X-tollX).*(V(i,2)<Y+tollY).*(V(i,2)>Y-tollY)+RES;
end
This "rough" solution works well with small meshgrids and V, but since i have to manage far bigger data sets i would like to vectorize and refine the code to get better performances.
P.S. I expected '&' operator to be faster in general than ' .* ', but this doesn't seem to be true, at least for my case.
To test this I simply changed the statement inside the for loop with this one:
RES=((V(i,1)<X+tollX)&(V(i,1)>X-tollX)&(V(i,2)<Y+tollY)&(V(i,2)>Y-tollY))|RES;

回答 (1 件)

Rik
Rik 2020 年 10 月 6 日
I would suggest using ismembertol, or consider functions like normxcorr2 from the image processing toolbox.
  3 件のコメント
Rik
Rik 2020 年 10 月 6 日
I'm not quite sure how implicit expansion (with bsxfun) would increase performance. I suspect ismembertol would be more efficient. I also don't necessarily see how find would be useful.
Alberto Belvedere
Alberto Belvedere 2020 年 10 月 6 日
Thanks, i'll do some tests to see which one performs better.

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

カテゴリ

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