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

2 次查看(过去 30 天)
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
...

回答(2 个)

Andrei Bobrov
Andrei Bobrov 2016-7-19
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 2016-7-19
编辑:Guillaume 2016-7-19
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.

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by