issues with function find()

5 Ansichten (letzte 30 Tage)
Camille
Camille am 2 Mär. 2013
I am using find() to find the index of a number in a matrix, but it is returning an empty matrix yet am sure that the number is part of that matrix. Here is the code
tr=0:.1:20;
b=find(tr==2.4)
and it is returning
b =
Empty matrix: 1-by-0
Please I need your help. What am I doing wrong or what should I do instead?

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 2 Mär. 2013
Try this
tr=0:.1:20;
b=find(tr==0.1*24)

Weitere Antworten (1)

Brian B
Brian B am 2 Mär. 2013
Bearbeitet: Brian B am 2 Mär. 2013
Try
tr=0:.1:20;
tr = round(tr*1e12)/1e12;
b=find(tr==2.4)
This is a problem with finite precision. See the explanation offered at http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F.
  1 Kommentar
Brian B
Brian B am 2 Mär. 2013
Actually, if you're using non-integer numbers, a better method is
b= find(abs(tr - 2.4)<1e-12)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by