How to find all datetimes that are within predefined time windows (plural!)?

Hi!
I have a table T with integers in the first column and datetimes in the second column and a cell-array TW of tables with 2 columns TW.start and TW.end datetimes of N rows/timewindows (N varies).
Now I want to find all datetimes in T that are within the time windows.
I know the command isbetween(t,tlower,tupper):
for ii=1:size(TW,1)
T(:,ii+1) = isbetween(T.Times,TW{ii}.start,TW{ii}.end)
end
This produces the error:
Error using datetime/isbetween>isbetweenUtil (line 91)
All inputs must have a common size, or be scalar.
I guess it is not possible to use isbetween with arrays (of same size) as tlower and tupper.
Is there an alternative direct function to solve my problem?
I mean, i could work around with a nested loop... or create new arrays with all minutewise datetimes between start and end and use ismember... But there must be a better way, which I don't know or can't think of at the moment.
Thanks in advance!

6 Comments

What are the heights of the table T and the tables stored in the cells of TW?
If those heights aren't all the same, what would you want to do in this case?
Tdates = datetime('today') + days([0; 1; 2])
Tdates = 3×1 datetime array
23-Jun-2021 24-Jun-2021 25-Jun-2021
TWdates = datetime('today') + days([-5 -4; -3 1; 5 6; -2 1.5])
TWdates = 4×2 datetime array
18-Jun-2021 00:00:00 19-Jun-2021 00:00:00 20-Jun-2021 00:00:00 24-Jun-2021 00:00:00 28-Jun-2021 00:00:00 29-Jun-2021 00:00:00 21-Jun-2021 00:00:00 24-Jun-2021 12:00:00
The first element of Tdates is in the range specified by the second and fourth rows of TWdates but none of the others. Does it need to be in the range specified by the first row of TWdates to be counted in or can it be in the range of any of the rows to count?
The third element of Tdates is outside of any of those intervals, but is between the first and last dates in TWdates. Does that make it in or out?
What if anything should be done with the fourth row of TWdates?
Info: TW gives me time windows, in which an influencing factor for my analyses meets specific conditions. Each of the tables stored in the cellarray represent a diffenrent condition. Now I want to get all events which Tdates are within these time windows of condition 1, all Tdates within the time windows of condition 2, and so on. I imagined an additioanal column for each condition in my Tdates with true/1 (in) or false/0 (out) (or an direct output of the indices) which shows me, if the datetimes are within any of the time windows of this condition.
1. What are the heights of the table T and the tables stored in the cells of TW?
  • The heights of table T and the tables in TW are not the same. The diffenrent tables stored in the cellarray TW are also not of uniform height (but the columns of each table are ofcourse of same height).
2. The first element of Tdates is in the range specified by the second and fourth rows of TWdates but none of the others. Does it need to be in the range specified by the first row of TWdates to be counted in or can it be in the range of any of the rows to count?
  • Time windows in my TW does not overlap.
  • Tdates can be in the range of any row (time window) to be set "true"
3. The third element of Tdates is outside of any of those intervals, but is between the first and last dates in TWdates. Does that make it in or out?
  • That would is an out.
4. What if anything should be done with the fourth row of TWdates?
  • Sorry, I do not know what you mean. There should not be done anything with the time windows.
I hope now it is more understandable. Thank you for your effort!
Why you want to use a function? You can use inequalities.
Maybe I make it too complicated with too many unnecessary details...
New approach:
Tdates = 1:10';
TW1 = [2, 5]';
TW2 = [7, 9]';
wanted_result = Tdates >=TW1(1) & Tdates <= TW1(2) | Tdates >=TW2(1) & Tdates <= TW2(2)
Is there a function, which gives me the same result?
Yeah but that is just the most simplest case. I have thousands of time windows for 17 conditions.
My intention is to improve my coding. So i was wandering, if there is a more direct and shorter way than something like this:
Tdates = [1:10]';
TW1 = [2, 5; 8, 9];
TW2 = [1, 1; 6, 7; 10, 10];
TW{1} = TW1;
TW{2} = TW2;
for ii = 1:size(TW,2)
for jj = 1:size(TW{ii},1)
for yy = 1:size(Tdates,1)
if Tdates(yy,1) >=TW{ii}(jj,1) & Tdates(yy,1) <= TW{ii}(jj,2)
Tdates(yy,ii+1) = true;
end
end
end
end

Sign in to comment.

Answers (0)

Categories

Tags

Asked:

on 23 Jun 2021

Edited:

on 24 Jun 2021

Community Treasure Hunt

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

Start Hunting!