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
3 次查看(过去 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
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!