필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by