フィルターのクリア

interval between colon-generated vector

1 回表示 (過去 30 日間)
Trevor Harris
Trevor Harris 2016 年 11 月 15 日
コメント済み: Trevor Harris 2016 年 11 月 15 日
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.
  1 件のコメント
Trevor Harris
Trevor Harris 2016 年 11 月 15 日
Thank you for your answer, very helpful.

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by