I cannot find the index of a specific element in an array?

I have an array of non-repeating elements, X. I need to find the index of a specific element, 1.5 in X. I am trying to do index=find(X==1.5). However it just does not work. I gives me a "empty matrix". Does this have to do with the elements in the array being decimal (float?) ? Please help me out.

댓글 수: 1

btw, I am absolutely sure that 1.5 and the other elements I've tried with this method is actually in array X.

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

답변 (1개)

Oleg Komarov
Oleg Komarov 2011년 7월 31일
Yes, floating point approximation ( faq6.1)
What you see is not what you get!
try this:
a = 0.2 + 0.1
>> a == 0.3
ans =
0
sprintf('%.17f',a)
% Compare with threshold
abs(a - 0.3) < eps

댓글 수: 3

ok I think I sort of understand what this is saying, but how does this help me? Like i get it that the sprintf function will print the float to like 17 digits, but how can I use this to equate the floating point numbers in my array X to 1.5?
Reading the faq won't hurt just because it's in the brackets.
@Robin: This is an evergreen question. Ask Google for "Matlab faq6.1" to find more.
@Oleg: +1.

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

카테고리

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

질문:

2011년 7월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by