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 个评论
Aaron Anderson
Aaron Anderson 2016-12-5
编辑:Aaron Anderson 2016-12-5
It's defined as a 1x1 double, and has a value of 0.0500
In the separate test script I wrote, I defined it with that same value, and it worked.

请先登录,再进行评论。

采纳的回答

James Tursa
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 个评论
Aaron Anderson
Aaron Anderson 2016-12-5
编辑:Aaron Anderson 2016-12-5
This does fix the linspace error. I hadn't considered that since it's a double it's not quite accurate (due to the bit level approximation: at least I think this is why). But how can I work around this for the usage of the colon to make the matrix? It's adding up that small round off error and then it doesn't quite fit into 240 so it won't make that point.
Aaron Anderson
Aaron Anderson 2016-12-5
Actually never mind, I fixed it by making it
0:sampRate:240+sampRate
So it would include the value at 240 and then chop the value at 240+sampRate due to the error in approximation. Thanks a lot for your help!

请先登录,再进行评论。

更多回答(1 个)

David Barry
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
Aaron Anderson 2016-12-6
The sampRate is a value being pulled from a file by a script that I didn't write. The issue must be that it's not quite perfectly represented. As below, I had to fix it using some changes to the code.
Walter Roberson
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 CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by