Why find cannot handle this very simple task?

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?

답변 (2개)

Stephan
Stephan 2021년 4월 19일

0 개 추천

You are dealing with doubles, they are not precisly 0.077 - use round:
X = -0.1:.001:.25;
find(round(X,3) == .077)

댓글 수: 3

Mr M.
Mr M. 2021년 4월 27일
ok, i see, but what about the right hand side, .077 is ok? round(.077,3) is not better?
Mr M.
Mr M. 2021년 4월 27일
I can not undersatnd: if .077 is good, than -0.1:.001:.25 is why not good? since elements of this vector is -0.100+0.001+0.001+etc., and each term is rounded to 3th order
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))
ans = 0.0770
Or work with integer values and convert to non-integer values as needed:
X2 = -100:250;
X2(X2 == 77)/1000
ans = 0.0770
Or use ismembertol which tries to choose a good "close enough" tolerance.
X(ismembertol(X, 0.077))
ans = 0.0770

댓글을 달려면 로그인하십시오.

Jan
Jan 2021년 4월 27일

0 개 추천

Welcome to the world of calculations with numbers represented with limited precision.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2021년 4월 19일

답변:

Jan
2021년 4월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by