Find function can not find vector element index

11 ビュー (過去 30 日間)
Md. Mahfuz Talukdar
Md. Mahfuz Talukdar 2020 年 7 月 2 日
I am trying a find an specific value from an array like below. But even if the value 67.2 is there the command window shows error.
>>range=1:0.1:100;
>>find(range==67.2)
1×0 empty double row vector
I have also tried many other work arounds but it seems to there is some inherent problem. Or there might not be. I am using MATLAB Online 2020a version.
Thanks in advance.

採用された回答

Geoff Hayes
Geoff Hayes 2020 年 7 月 2 日
You can't use the equality operator == with doubles. See Floating-Point Numbers for details. Instead you need to use a tolerance check when comparing. So something like
find(abs(range - 67.2) < 0.00001)
In the above, we look at the absolute difference between the elements of the range array and 67.2. If that difference is less than some tolerance, then we consider the two numbers to be "close enough" to each other.
  1 件のコメント
Md. Mahfuz Talukdar
Md. Mahfuz Talukdar 2020 年 7 月 2 日
Thanks a lot. It works perfectly.

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

その他の回答 (0 件)

カテゴリ

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