addtodate creates too many milliseconds

1 visualización (últimos 30 días)
d_moan
d_moan el 3 de Ag. de 2016
Comentada: d_moan el 28 de Ag. de 2016
I wanted to create a vector of all milliseconds in a day, so I wrote
0:addtodate(0,1,'millisecond'):(1-addtodate(0,1,'millisecond'));
which resulted in a vector with 86767016 elements, instead of the expected 24*60*60*1000 = 86400000 ones.
The same with seconds worked just fine.
I assume this is due to some roundoff error, and of course I could just write (0:86400000-1)/86400000, but I was wondering if this should be considered a bug.
Also was wondering what would be the better approach in the future when adding several milliseconds - I can think of three options:
addtodate(0,1,'millisecond') + addtodate(0,1,'millisecond');
2*addtodate(0,1,'millisecond');
2/86400000;
(The last one giving a different result than the first two)

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 3 de Ag. de 2016
Use linspace
a=linspace(0,1-addtodate(0,1,'millisecond'),86400000);
  2 comentarios
Steven Lord
Steven Lord el 3 de Ag. de 2016
Or colon:
msPerDay = milliseconds(days(1));
y = milliseconds(0:1:msPerDay-1);
d_moan
d_moan el 28 de Ag. de 2016
Hey guys, thanks for the quick responses, sorry for my belated one.
@Steven, looks like a great option, but unfortunately I can't use it due to my Matlab version. Also, had you opened it as a different answer instead of a comment I could've at least upvoted it.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by