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

1 vue (au cours des 30 derniers jours)
Tushar Patel
Tushar Patel le 19 Juil 2016
Modifié(e) : Guillaume le 19 Juil 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
...

Réponses (2)

Andrei Bobrov
Andrei Bobrov le 19 Juil 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 le 19 Juil 2016
Modifié(e) : Guillaume le 19 Juil 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.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by