Filter löschen
Filter löschen

How to concatenate (take cross product of) two datetime arrays?

2 Ansichten (letzte 30 Tage)
Tushar Patel
Tushar Patel am 19 Jul. 2016
Bearbeitet: Guillaume am 19 Jul. 2016
I have two arrays
Date = [datetime(2016,4,5), datetime(2016,5,4), datetime(2016,6,7)];
Time = duration(0,0:30:90,0,'Format', 'hh:mm:ss');
I want a product of these two arrays such that result is
ans =
5-Apr-2016 00:00:00
5-Apr-2016 00:30:00
5-Apr-2016 01:00:00
5-Apr-2016 01:30:00
4-May-2016 00:00:00
4-May-2016 00:30:00
4-May-2016 01:00:00
...

Antworten (2)

Andrei Bobrov
Andrei Bobrov am 19 Jul. 2016
Date = [datenum(2016,4,5), datenum(2016,5,4), datenum(2016,6,7)];
a = bsxfun(@plus,Date,1/48*(0:3)');
out = datestr(a,'dd-mmm-yyyy HH:MM:SS');

Guillaume
Guillaume am 19 Jul. 2016
Bearbeitet: Guillaume am 19 Jul. 2016
This avoids conversion to datenum:
Date = [datetime(2016,4,5), datetime(2016,5,4), datetime(2016,6,7)];
Time = duration(0,0:30:90,0,'Format', 'hh:mm:ss');
[tidx, didx] = ndgrid(1:numel(Time), 1:numel(Date));
result = reshape(Date(didx) + Time(tidx), [], 1)
ndgrid is typically used to obtain the cartesian product of two sets.

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by