Issue with find() function

9 ビュー (過去 30 日間)
Mitchell Frankel
Mitchell Frankel 2014 年 6 月 5 日
回答済み: Image Analyst 2014 年 6 月 5 日
If I create an array x = 0.1:0.05:0.6; and then ask find(x==0.15), I get an empty matrix. It also fails for x==0.40? I don't understand this at all.
  1 件のコメント
Cedric
Cedric 2014 年 6 月 5 日
If you understand the answers below, read about EPS. In the end, you will probably end up doing something like:
pos = find( abs(x - 0.15) < eps(1) ) ;

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

採用された回答

Star Strider
Star Strider 2014 年 6 月 5 日
It’s called ‘floating point approximation error’. The concept is similar to the decimal approximation of 1/3 = 0.33333.... To get around it, you need to state the conditions in your find calls to allow for some imprecision.
Run these to get an idea of how the concept applies to your problem:
x = 0.1:0.05:0.6;
ix11 = find(x <= 0.15, 1, 'last')
ix12 = find(x >= 0.15, 1, 'first')
x1 = x([ix11 ix12])
ix21 = find(x <= 0.40, 1, 'last')
ix22 = find(x >= 0.40, 1, 'first')
x2 = x([ix21 ix22])

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 6 月 5 日

Image Analyst
Image Analyst 2014 年 6 月 5 日
Here's some pretty general code based on the FAQ Azzi referred you to:
x = 0.1 : 0.05 : 0.6 % Sample data
targetValue = 0.15 % Whatever value you want to find
tolerance = .005 % We want to find x within this value of the target.
% Find values in range.
indexesInRange = abs(x - targetValue) <= tolerance
xThatAreInRange = x(indexesInRange)
% Or, in an if statement, if you want to use it in an if statement instead...
if abs(x(2) - targetValue) <= tolerance
% x(2) is close enough to the targetValue.
else
% x(2) is far away from the targetValue.
end

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by