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.

 採用された回答

José-Luis
José-Luis 2012 年 10 月 25 日
編集済み: José-Luis 2012 年 10 月 25 日

1 投票

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);

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

質問済み:

2012 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by