Why find cannot handle this very simple task?
2 ビュー (過去 30 日間)
古いコメントを表示
X = -0.1:.001:.25;
find(X == .077)
I get the following:
ans = 1×0 empty double row vector
However X(178) = 0.077. How to get back index 178?
0 件のコメント
回答 (2 件)
Stephan
2021 年 4 月 19 日
You are dealing with doubles, they are not precisly 0.077 - use round:
X = -0.1:.001:.25;
find(round(X,3) == .077)
3 件のコメント
Steven Lord
2021 年 4 月 27 日
I wouldn't use round here. Decide how close is "close enough" and use that as the tolerance.
X = -0.1:0.001:0.25;
closeEnough = 1e-8;
X(find(abs(X-0.077) < closeEnough))
Or work with integer values and convert to non-integer values as needed:
X2 = -100:250;
X2(X2 == 77)/1000
Or use ismembertol which tries to choose a good "close enough" tolerance.
X(ismembertol(X, 0.077))
Jan
2021 年 4 月 27 日
Welcome to the world of calculations with numbers represented with limited precision.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!