How to give time interval with different parenthesis in matlab?
Show older comments
time = [0, 2) with sampling interval of 0.005
i try with t = 0:0.005:2; but this includes '2' also so can anyone help me how to exclude 2 from the plot?
1 Comment
Walter Roberson
on 1 Oct 2012
Please read the guide to tags and retag this question; see http://www.mathworks.co.uk/matlabcentral/answers/43073-a-guide-to-tags
Answers (3)
Image Analyst
on 1 Oct 2012
The others gave you good answers. I just want to notify you of another, related function that you may find useful someday: linspace(). It is particularly useful when you know the number of elements, but not the increment - the converse of your situation:
% Make an array of 400 elements between 0 (inclusive) and 2 (NOT inclusive).
t = linspace(0, 2-eps, 400);
6 Comments
per isakson
on 1 Oct 2012
Floating point rounding error is not our friend
>> t = linspace(0, 2-eps, 400); t(end)
ans =
2
>>
Image Analyst
on 1 Oct 2012
Yes, the display can be deceptive because it rounds things to 4 decimal places for display. But the last one is not really 2, even though it looks like it.
>> t(end) == 2
ans =
0
so in the math it wouldn't be 2 but in the display it does look like 2, even though it's not. Of course, if one knew they wanted to end at 1.995 instead of infinitesimally close to 2, one could do
t = linspace(0, 1.995, 400);
or use whatever starting and ending value they want.
per isakson
on 1 Oct 2012
Edited: per isakson
on 1 Oct 2012
I stand corrected. However, what exactly does
time = [0, 2) with sampling interval of 0.005
mean i a digital world? To me it means 0,0.005, ...,1.955, which implies that the sampling interval is more important than the upper limit.
K>> t = linspace(0, 2-eps(), 400); t(200)
ans =
0.99749
Image Analyst
on 1 Oct 2012
Yes, for him right now, the interval was most likely more important, hence my initial answer that your and Azzi's solutions were good, and that linspace() was merely another function that may come in useful someday.
Erick Montes
on 11 Sep 2021
Jesus Christ, I thank you for this inside tip sir; I have the world's worst MATLAB teacher. Where do I tip you? thinking emoji*
Image Analyst
on 11 Sep 2021
@Erick Montes, to reward contributors like me with "reputation points", you can click the "Vote" icon at the top of my Answer. Thanks in advance.
Azzi Abdelmalek
on 1 Oct 2012
Edited: Azzi Abdelmalek
on 1 Oct 2012
t = 0:0.005:2-0.005
or
t = 0:0.005:2
t=t(1:end-1)
per isakson
on 1 Oct 2012
Edited: per isakson
on 1 Oct 2012
Try
>> t = 0 : 0.0005 : 2-eps(2e3); t(end)
ans =
1.9995
>>
or
>> t = 0 : 0.0005 : 2; t(end)=[]; t(end)
ans =
1.9995
>>
AFAIK: There is no elegant way to do it.
IMO: The most robust way is to create the vector including the upper limit and delete it in a separate step. Matlab assumes we want the upper limit included :]
1 Comment
Image Analyst
on 1 Oct 2012
per, correct it to 0.005 and then I'll delete this comment.
Categories
Find more on General Applications 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!