what's wrong with 'smooth' and 'find' commands in this code?

5 vues (au cours des 30 derniers jours)
Taehwan
Taehwan le 31 Jan 2012
x = [0 0 0 1 1 1 0 1 0 0 1 1 1 0 0 0 0];
y = smooth(x,5);
find(y(:) == 0.6)
ans = Empty matrix: 0-by-1
Running above script gives empty matrix though definitely 0.6 elements exit. What have I done wrong?

Réponse acceptée

Grzegorz Knor
Grzegorz Knor le 31 Jan 2012
Look at result of this line:
y- round(y*10)/10
In vector y there is no exact 0.6 value.
Try this code:
x = [0 0 0 1 1 1 0 1 0 0 1 1 1 0 0 0 0];
y = smooth(x,5);
find(abs(y(:)-0.6)<eps)
  2 commentaires
Taehwan
Taehwan le 31 Jan 2012
Thank you for the comment and the results. One thing I still can't catch is that why 'smooth' doesn't give exact 0.6 (= 3/5) answer... while others are exact 0.2, 0.4, 0.8, etc.
Grzegorz Knor
Grzegorz Knor le 31 Jan 2012
It is 0.6, because difference between result and exact value is 1.11022302462516e-16 (on my PC) and it is smaller than floating-point relative accuracy 2.22044604925031e-16 (on my PC).
Compare with:
format long
x = single([0 0 0 1 1 1 0 1 0 0 1 1 1 0 0 0 0])
y = smooth(x,5)

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 31 Jan 2012
This is one of the mayn effects of the limited precision of floating point values. See: Answers: Tag=faq6.1.

Catégories

En savoir plus sur Matrix Indexing 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