How to give time interval with different parenthesis in matlab?

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?

Respuestas (3)

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 comentarios

Floating point rounding error is not our friend
>> t = linspace(0, 2-eps, 400); t(end)
ans =
2
>>
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
per isakson el 1 de Oct. de 2012
Editada: per isakson el 1 de Oct. de 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
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.
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*
@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.

Iniciar sesión para comentar.

Azzi Abdelmalek
Azzi Abdelmalek el 1 de Oct. de 2012
Editada: Azzi Abdelmalek el 1 de Oct. de 2012
t = 0:0.005:2-0.005
or
t = 0:0.005:2
t=t(1:end-1)
per isakson
per isakson el 1 de Oct. de 2012
Editada: per isakson el 1 de Oct. de 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 :]

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 1 de Oct. de 2012

Comentada:

el 11 de Sept. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by