I even wrote another script that does the exact same code as above, and it works fine. I'll also note that I have never had a problem with colon operators outside of this set of scripts.
Making a matrix using colons or linspace working improperly within code
조회 수: 4 (최근 30일)
이전 댓글 표시
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
James Tursa
2016년 12월 5일
편집: James Tursa
2016년 12월 5일
What are the values of sampRate that you are using?
채택된 답변
James Tursa
2016년 12월 5일
편집: James Tursa
2016년 12월 5일
I suspect this is being caused by floating point round off error in the various calculations. Maybe just use a round in your linspace calculation:
linspace(0,1/sampRate/2,round(10/sampRate/2 + 1))
As a general comment, I will advise you that the exact same code can run slightly differently depending on how it is run. E.g., I have seen cases where an expression in a function will produce one answer when called normally, but will produce a different answer when called by stepping into the line while in the debugger. Annoying, but that's the way it is. In one case the optimizer rearranged/changed things slightly that it couldn't in the case of stepping though line by line. So that may explain how you are getting different answers depending on how you are running the code.
댓글 수: 2
추가 답변 (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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!