Making an array Fails for mysterious reason (small stepsize)
Afficher commentaires plus anciens
In short:
fs = 50000;
x1 = (0:1/fs:1);
find(x1==0.5)
This works fine. The value of 0.5 is found in the array.
fs = 50000;
x2 = ( 0:1/fs:(1-1/fs) );
find(x2==0.5)
Does not work. The value of 0.5 is not there anymore and the find-function returns an empty matrix. I would have expected that x2 would be the equivalent of x1(1:end-1) but that does not seem to be the case. For smaller values fs (e.g. 5000) everything is fine. Can someone tell me what is going on exactly?
Thanks
Réponse acceptée
Plus de réponses (1)
KSSV
le 21 Sep 2016
1 vote
It is not advised to compare floating point numbers using ==. You have to fix a small number (tolerance) say, eps = 10^-5 and check for is the absolute difference less then that. If so then the floating point numbers are close enough. '
More on:
1 commentaire
Catégories
En savoir plus sur Multidimensional Arrays 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!