How can I find the index of a point in an array?

13 ビュー (過去 30 日間)
Agustin
Agustin 2017 年 7 月 11 日
コメント済み: Star Strider 2017 年 7 月 12 日
Hi! I have the following point: tmp = [-119.3 1042.5] and I want the index of this point in a grid. I have two separate grids, xdata and ydata and I'm trying to find the indexes, ipix and jpix. When I'm looking for ipix using
ipix = find(xdata == tmp(1,1))
I get the following:
ipix =
Empty matrix: 0-by-1
The numbers on the xdata and ydata grids are the form -1.193846913204592e+02 and when I write
ipix = find(xdata == -1.193846913204592e+02)
I get
ipix =
71361
which makes no sense. Can somebody help me with this? What I need is to find the index of the given point so I can find the displacement of the point in different images.
Thank you;

採用された回答

Star Strider
Star Strider 2017 年 7 月 11 日
This is due to floating-point approximation error. You have to search within a range of tolerances.
Try something like this:
tol = 0.001;
[xrow,xcol] = find((xdata >= tmp(1,1)-tol) & (xdata <= tmp(1,1)+tol));
[yrow,ycol] = find((xdata >= tmp(1,2)-tol) & (xdata <= tmp(1,2)+tol));
For a more extended discussion, see Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero? (link)
  4 件のコメント
Agustin
Agustin 2017 年 7 月 12 日
Thanks!
Star Strider
Star Strider 2017 年 7 月 12 日
My pleasure!

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

その他の回答 (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