Making a matrix using colons or linspace working improperly within code
이전 댓글 표시
I have a very strange issue with my code. For some reason, in my code, where I have written
timeVector = 0:sampRate:240
the timeVector variable is excluding the last element of the matrix (240). So it only runs up to but not including the last value. However, when I execute that same command outside of my code, it includes that last value (as it should). This is very confusing to me. Nowhere in my code have I somehow redefined what the colons should do when I try to make a matrix in this fashion. Does anybody have a clue what's going on here?
Also, if I set a breakpoint in my code in this region, and then call the same thing that the line above states, it recreates the same issue of not including 240.
And in the shell (not written in a script), if I define sampRate as the same value that it's defined as in my code, and then I call the exact same line as above, it includes 240.
I have actually encountered this issue once before, when I was trying to use linspace. The example is below:
linspace(0,1/sampRate/2,10/sampRate/2 + 1)
was giving me a matrix that was 1 element shorter than the specified n = (10/sampRate/2 +1) points. I was so confused by this, and once again, outside of my code, the linspace would work properly and make the specified number of points. I ended up getting fed up with trying to troubleshoot the issue and now the code reads
linspace(0,1/sampRate/2,10/sampRate/2 + 2)
because I could not find any way to fix it, so by increasing n by another 1, I received a matrix with the desired number of elements.
What is going on here? The two issues I have detailed above are from a set of .m files, the first issue I wrote about is within a function that the script that the second issue arises in calls.
댓글 수: 3
Aaron Anderson
2016년 12월 5일
편집: Aaron Anderson
2016년 12월 5일
James Tursa
2016년 12월 5일
편집: James Tursa
2016년 12월 5일
What are the values of sampRate that you are using?
Aaron Anderson
2016년 12월 5일
편집: Aaron Anderson
2016년 12월 5일
채택된 답변
추가 답변 (1개)
David Barry
2016년 12월 5일
I suspect there is nothing wrong with your MATLAB here and you just need to take a look at what your sample rate is and then think again about what you are asking MATLAB to do. For example, if I ask for an array from 0 to 5 in 0.3 steps I am not going to get the end value equal to 5 because you can't divide 5 by 0.3!
a = 0:0.3:5
댓글 수: 4
Aaron Anderson
2016년 12월 5일
David Barry
2016년 12월 5일
Have you confirmed it is exactly 0.05? Have you explicitly defined it as 0.05 or is it calculated? Try putting this just before and see if it is true or false.
isequal(sampRate, 0.05)
Aaron Anderson
2016년 12월 6일
Walter Roberson
2016년 12월 6일
It is not possible to represent 0.05 exactly in binary floating point.
linspace() compensates for the effects of accumulated floating point round-off of the ':' operator.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!