Effacer les filtres
Effacer les filtres

interval between colon-generated vector

1 vue (au cours des 30 derniers jours)
Trevor Harris
Trevor Harris le 15 Nov 2016
Commenté : Trevor Harris le 15 Nov 2016
Hey all,
I'm getting some weird behavior when I'm creating a vector using the colon function. One would expect if I do a diff function on a vector, it would return a single value, being equal to the second input of colon. I seem to be getting some floating-point instability. Please see the attached screenshot which will better explain the problem. Any suggestions as to what may be happening?
Thanks! Trevor
  2 commentaires
John D'Errico
John D'Errico le 15 Nov 2016
Please don't post a screenshot like that. It is fuzzy and impossible to read.
You can copy and paste in text. Is that any more difficult. You can also attach a .jpg of the figure, which you know how to do, since you inserted the impossible to read screenshot.
Trevor Harris
Trevor Harris le 15 Nov 2016
My apologies, this line will generate the plot I'm referring to.
plot(diff(0:7.2:640000))

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 15 Nov 2016
Modifié(e) : Jan le 15 Nov 2016
This is explained in the FAQ, because it is a frequently asked question:
So you see, the observed behavior is not "weird", but expected if you consider the limited precision of floating point values according to the IEEE754 standard.
  1 commentaire
Trevor Harris
Trevor Harris le 15 Nov 2016
Thank you for your answer, very helpful.

Connectez-vous pour commenter.

Plus de réponses (2)

John D'Errico
John D'Errico le 15 Nov 2016
Why should diff return a single unique value? That presumes that floating point arithmetic is able to store all floating point numbers exactly. Since the industry standard IEEE formats are used in MATLAb to store floating point numbers, all such numbers are in BINARY.
You cannot represent the number 0.1 (i.e., 1/10) in binary, just as you cannot represent the number 1/3 exactly in decimal form.
There is no "instability". It is merely a reflection of what you need to learn about floating point numbers.
  1 commentaire
Trevor Harris
Trevor Harris le 15 Nov 2016
I presumed that diff would return a single value because the space between each value in the vector should be the same in a colon-generated vector. I see from your answer this isn't necessarily the case.
plot(diff(0:7.2:640000))

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 15 Nov 2016
diff() returns the difference between ALL elements of a vector - between the 2nd and 1st, between the 3rd and 2nd, between the 4th and 3rd, between the 5th and 4th, and so on. Perhaps you thought (incorrectly) that if the difference between all your elements was the same it would average all those differences in your vector and give you the average of the differences. That is not how diff() operates. Since you passed it a vector of N elements, it will give you a vector of N-1 differences back, not a single number.
  1 commentaire
Trevor Harris
Trevor Harris le 15 Nov 2016
Yes, I understand how the diff works, but I see the error in my question. I didn't mean a single value, I meant the same value. i.e.,
unique(diff(0:7.2:640000)) should be scalar - but its not.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by