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

1 visualizzazione (ultimi 30 giorni)
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
...

Risposte (2)

Andrei Bobrov
Andrei Bobrov il 19 Lug 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 il 19 Lug 2016
Modificato: Guillaume il 19 Lug 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.

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by