Is there something wrong with the find function?

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.

 Réponse acceptée

José-Luis
José-Luis le 25 Oct 2012
Modifié(e) : José-Luis le 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);

Plus de réponses (0)

Catégories

En savoir plus sur Performance and Memory dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by