Clear Filters
Clear Filters

interval between colon-generated vector

1 view (last 30 days)
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 Comments
John D'Errico
John D'Errico on 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 on 15 Nov 2016
My apologies, this line will generate the plot I'm referring to.
plot(diff(0:7.2:640000))

Sign in to comment.

Accepted Answer

Jan
Jan on 15 Nov 2016
Edited: Jan on 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.

More Answers (2)

John D'Errico
John D'Errico on 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 Comment
Trevor Harris
Trevor Harris on 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))

Sign in to comment.


Image Analyst
Image Analyst on 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 Comment
Trevor Harris
Trevor Harris on 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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!