Making a matrix using colons or linspace working improperly within code
Show older comments
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 Comments
Aaron Anderson
on 5 Dec 2016
Edited: Aaron Anderson
on 5 Dec 2016
James Tursa
on 5 Dec 2016
Edited: James Tursa
on 5 Dec 2016
What are the values of sampRate that you are using?
Aaron Anderson
on 5 Dec 2016
Edited: Aaron Anderson
on 5 Dec 2016
Accepted Answer
More Answers (1)
David Barry
on 5 Dec 2016
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 Comments
Aaron Anderson
on 5 Dec 2016
David Barry
on 5 Dec 2016
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
on 6 Dec 2016
Walter Roberson
on 6 Dec 2016
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.
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!