interval between colon-generated vector

2 次查看(过去 30 天)
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 个评论
John D'Errico
John D'Errico 2016-11-15
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 2016-11-15
My apologies, this line will generate the plot I'm referring to.
plot(diff(0:7.2:640000))

请先登录,再进行评论。

采纳的回答

Jan
Jan 2016-11-15
编辑:Jan 2016-11-15
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.

更多回答(2 个)

John D'Errico
John D'Errico 2016-11-15
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 个评论
Trevor Harris
Trevor Harris 2016-11-15
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))

请先登录,再进行评论。


Image Analyst
Image Analyst 2016-11-15
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 个评论
Trevor Harris
Trevor Harris 2016-11-15
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by