Is there something wrong with the find function?

2 views (last 30 days)
t=-0.8:0.001:0.8; find(t==0.2)%try to find out the index of t=0.2; See the result. And it is empty. I think it is a bug of the Matlab. Then try to type. find(t==0); the result will show up. If change t=-0.6:0.001:1; find(t==0.2)will be show up. I am not sure what is wrong with the Matlab or if there is something wrong with my understanding about the function.

Accepted Answer

José-Luis
José-Luis on 25 Oct 2012
Edited: José-Luis on 25 Oct 2012
From the documentation on the colon operator:
j:i:k is the same as [j,j+i,j+2i, ...,j+m*i]
In your vector 0.2 would be -0.8+1000*0.001. Due to numerical precision, and the way in which multiplication and addition are performed, that is not quite equal to 0.2.
Just try
-0.8+1000*0.001 == 0.2
And you will see what I mean. Find is working just fine. You might want to use
find(t <= 0.2+eps & t >= 0.2-eps);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!