I am not certain what result you want.
This computes all values of ‘t.data’ that are within 18 hours of a particular row value:
data = {"2017-01-12 09:50:46" "2017-01-15 13:50:46" "2017-01-15 14:50:50" "2017-01-15 17:52:06" "2017-01-15 18:52:22" "2017-01-19 09:52:22"}';
t = cell2table(data);
t.data = datetime(t.data, 'InputFormat', 'yyyy-MM-dd HH:mm:ss');
h18 = t.data + hours(18);
for k = 1:size(t.data,1)
within18{k,:} = t.data((t.data >= t.data(k)) & (t.data <= h18(k)));
end
For example:
Within18_3 = [within18{3}]
contains:
Within18_3 =
3×1 datetime array
15-Jan-2017 14:50:50
15-Jan-2017 17:52:06
15-Jan-2017 18:52:22
The first row in ‘Within18_3’ are the third row value and the next two rows are those within 18 hours of it.
EDIT — (5 Mar 2020 at 17:36)
If you want ±9 hours instead, this works:
h18 = [t.data + hours(-9), t.data + hours(9)];
for k = 1:size(t.data,1)
within18{k,:} = t.data((t.data >= h18(k,1)) & (t.data <= h18(k,2)));
end
For example:
Within18_3 = [within18{3}]
now contains:
Within18_3 =
4×1 datetime array
15-Jan-2017 13:50:46
15-Jan-2017 14:50:50
15-Jan-2017 17:52:06
15-Jan-2017 18:52:22
Best Answer