How do I solve the following problem with rounding?

3 vues (au cours des 30 derniers jours)
Nikola Mirkov
Nikola Mirkov le 21 Oct 2014
Modifié(e) : Jan le 21 Oct 2014
I run the following code:
clear all
x=-2:0.1:2;
find(x==0.9)
I get:
ans = Empty matrix: 1-by-0
How can I fix this problem? The version I use is R2013a.
Thanks! niko
  1 commentaire
Jan
Jan le 21 Oct 2014
Modifié(e) : Jan le 21 Oct 2014
Avoid the clear all, because this is more brute than useful.
This is one of the most frequently asked questions. I suggest to read the other ones also: http://matlab.wikia.com/wiki/FAQ

Connectez-vous pour commenter.

Réponses (2)

Amir
Amir le 21 Oct 2014
Modifié(e) : Amir le 21 Oct 2014
Please try this:
x=-2:0.1:2;
find(abs(x-0.9) < eps)
ans =
30

Pierre Benoit
Pierre Benoit le 21 Oct 2014
You can use a tolerance :
find(abs(x-0.9) < tol)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by