Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

datetime - Expand a datenum vector

I am trying to create a datevec series of dates and times, spaced by 20 minutes, with the minutes column being exactly 0, 20 and 40 with a given starting and ending time. First I create a 20 minute difference value

datenum_20_min_diff = datenum( [ 2021 , 1 , 27 , 17 , 0 ] ) - datenum( [ 2021 , 1 , 27 , 16 , 40 ] ) ;

and then try to create the required datevec series

s = datevec( ( datenum([2021,1,27,22,0]) : datenum_20_min_diff : datenum([2021,1,28,11,0]) )' ) ;

which works for the first few entries but then drifts from what I expect. What I get is (with the minutes column slightly tidied up for ease of viewing)

2.0210e+03   1.0000e+00   2.7000e+01   2.2000e+01    0            0
2.0210e+03   1.0000e+00   2.7000e+01   2.2000e+01   20            0
2.0210e+03   1.0000e+00   2.7000e+01   2.2000e+01   40            0
2.0210e+03   1.0000e+00   2.7000e+01   2.3000e+01    0            0
2.0210e+03   1.0000e+00   2.7000e+01   2.3000e+01   19   6.0000e+01
2.0210e+03   1.0000e+00   2.7000e+01   2.3000e+01   39   6.0000e+01
2.0210e+03   1.0000e+00   2.7000e+01   2.3000e+01   59   6.0000e+01

and what I want/expected is

2.0210e+03   1.0000e+00   2.7000e+01   2.2000e+01    0   0
2.0210e+03   1.0000e+00   2.7000e+01   2.2000e+01   20   0
2.0210e+03   1.0000e+00   2.7000e+01   2.2000e+01   40   0
2.0210e+03   1.0000e+00   2.7000e+01   2.3000e+01    0   0
2.0210e+03   1.0000e+00   2.7000e+01   2.3000e+01   20   0
2.0210e+03   1.0000e+00   2.7000e+01   2.3000e+01   40   0       
2.0210e+03   1.0000e+00   2.7000e+01   2.3000e+01    0   0

What am I doing wrong?

question from:https://stackoverflow.com/questions/65938193/expand-a-datenum-vector

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is no 'drift'. It's just that at some point your 20 minute interval starts getting expressed as 19 minutes and 60 seconds (=20 minutes).

I don't know why, but in theory the two are equivalent and you should not care about it. If you are worried about 'formatting' the date to obtain an output that is more visually meaningful, use a formatting function. E.g. datestr(s) will give you what you expect.

PS: "20 minutes expressed as fraction of a 24h-day" is as simple 20 / 24 / 60. No real need for the first line.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...