how to calculate common dates from two date arrays?

66 vues (au cours des 30 derniers jours)
Sven Larsen
Sven Larsen le 17 Oct 2025 à 15:23
Commenté : dpb le 19 Oct 2025 à 13:08
I have two arrays with many datenums in format [startTime endTime]. I am trying to find a way to calculate all common datetimes (i.e duration), and end product should be new array where is all [startTime endTime] of all common datetimes.
Example (datenums are datetime for clarity in this example): dates1 array some row is :
[1.10.2025 10:00:00, 1.10.2025 13:00:00]
and some rows in dates2 are:
[1.10.2025 09:30:00, 1.10.2025 11:00:00]
[1.10.2025 12:30:00 ,1.10.2025 14:40:00]
so new array of common times will get rows
[1.10.2025 10:00:00, 1.10.2025 11:00:00]
[1.10.2025 12:30:00, 1.10.2025 13:00:00]
Tried to ask Grok but its function was erroneus. Greatly appreciate help!
  5 commentaires
Dyuman Joshi
Dyuman Joshi le 17 Oct 2025 à 16:57
"they are all datenums and for example unique([dates1.dStrt]); gives 339 unique values...."
Yes, it escaped me that they can be datenums().
It seems I am missing something -
load('dates.mat')
whos
Name Size Bytes Class Attributes ans 1x33 66 char dates1 339x1 86825 struct dates2 351x1 89897 struct
dates1
dates1 = 339×1 struct array with fields:
dStrt dStp
y = dates1.dStrt;
size(y)
ans = 1×2
1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
z = vertcat(dates1.dStrt);
size(z)
ans = 1×2
339 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
"same algorithm works finding common number ranges also :)"
Similar logic will work. Same algorithm might not.
Please check Star Strider's answer below.
dpb
dpb le 18 Oct 2025 à 12:21
@Sven Larsen - convert the datenum arrays to datetimes and then see isbetween

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 18 Oct 2025 à 16:36
Modifié(e) : dpb le 19 Oct 2025 à 13:05
load dates
clear ans
dates1=[datetime(vertcat(dates1.dStrt),'convertfrom','datenum') datetime(vertcat(dates1.dStp),'convertfrom','datenum')];
tD1=array2table(dates1,'VariableNames',{'Start','Stop'});
tD1.Length=tD1.Stop-tD1.Start;
head(tD1)
Start Stop Length ____________________ ____________________ _________ 19-Mar-2024 16:10:53 19-Mar-2024 16:10:45 -00:00:08 21-Mar-2024 06:06:46 19-Mar-2024 16:10:53 -37:55:53 21-Mar-2024 08:28:59 21-Mar-2024 06:06:46 -02:22:13 23-Mar-2024 22:25:16 21-Mar-2024 08:28:59 -61:56:17 24-Mar-2024 11:33:38 23-Mar-2024 22:25:16 -13:08:22 24-Mar-2024 16:57:32 24-Mar-2024 11:33:38 -05:23:54 24-Mar-2024 16:59:15 24-Mar-2024 16:57:32 -00:01:43 24-Mar-2024 20:42:44 24-Mar-2024 16:59:15 -03:43:29
Ooops!!! The start/stop times appear to be reversed as the length of each time span is negative.,,,we'll just recreate the table as expected.
tD1=array2table(dates1,'VariableNames',{'Stop','Start'});
tD1.Length=tD1.Stop-tD1.Start;
head(tD1)
Stop Start Length ____________________ ____________________ ________ 19-Mar-2024 16:10:53 19-Mar-2024 16:10:45 00:00:08 21-Mar-2024 06:06:46 19-Mar-2024 16:10:53 37:55:53 21-Mar-2024 08:28:59 21-Mar-2024 06:06:46 02:22:13 23-Mar-2024 22:25:16 21-Mar-2024 08:28:59 61:56:17 24-Mar-2024 11:33:38 23-Mar-2024 22:25:16 13:08:22 24-Mar-2024 16:57:32 24-Mar-2024 11:33:38 05:23:54 24-Mar-2024 16:59:15 24-Mar-2024 16:57:32 00:01:43 24-Mar-2024 20:42:44 24-Mar-2024 16:59:15 03:43:29
dates2=[datetime(vertcat(dates2.dStrt),'convertfrom','datenum') datetime(vertcat(dates2.dStp),'convertfrom','datenum')];
tD2=array2table(dates2,'VariableNames',{'Stop','Start'});
tD2.Length=tD2.Stop-tD2.Start;
head(tD2)
Stop Start Length ____________________ ____________________ ________ 19-Mar-2024 16:10:53 19-Mar-2024 16:10:43 00:00:10 21-Mar-2024 06:06:32 19-Mar-2024 16:10:53 37:55:39 21-Mar-2024 08:29:13 21-Mar-2024 06:06:32 02:22:41 23-Mar-2024 22:25:14 21-Mar-2024 08:29:13 61:56:01 24-Mar-2024 00:33:46 23-Mar-2024 22:25:14 02:08:32 24-Mar-2024 02:00:09 24-Mar-2024 00:33:46 01:26:23 24-Mar-2024 11:33:29 24-Mar-2024 02:00:09 09:33:20 24-Mar-2024 20:42:46 24-Mar-2024 11:33:29 09:09:17
Now it's not quite clear to me what you actually want as a result -- for each start time in tD1 the records that are in the duration of that time span whose start time is within that length of time or which overlaps or ...?
Can finish up once have a clear definition.
ixoverlap=arrayfun(@(t_s,t_e)(t_s<=tD2.Stop)&(t_e>=tD2.Start),tD1.Start,tD1.Stop,'uni',0);
ix=ixoverlap{1};
for i=2:numel(ixoverlap)
ix=ix|ixoverlap{i};
end
res=tD1(ix,:);
height(res)
ans = 10
res
res = 10×3 table
Stop Start Length ____________________ ____________________ ________ 19-Mar-2024 16:10:53 19-Mar-2024 16:10:45 00:00:08 21-Mar-2024 06:06:46 19-Mar-2024 16:10:53 37:55:53 21-Mar-2024 08:28:59 21-Mar-2024 06:06:46 02:22:13 23-Mar-2024 22:25:16 21-Mar-2024 08:28:59 61:56:17 24-Mar-2024 11:33:38 23-Mar-2024 22:25:16 13:08:22 24-Mar-2024 16:57:32 24-Mar-2024 11:33:38 05:23:54 24-Mar-2024 16:59:15 24-Mar-2024 16:57:32 00:01:43 24-Mar-2024 20:42:44 24-Mar-2024 16:59:15 03:43:29 25-Mar-2024 09:16:45 24-Mar-2024 20:42:44 12:34:01 27-Mar-2024 16:26:51 25-Mar-2024 09:16:45 55:10:06
I THINK the above will find those that are overlapping; check the results to see if are what you were looking for.
  1 commentaire
dpb
dpb le 19 Oct 2025 à 13:08
ERRATUM/ADDENDUM
NOTA BENE: I forgot to remove the 1:10 debugging short subset subscripts on the arguments in the arrayfun() line above; I just made the edit to remove but the result will be many more than 10, I'm certain.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Calendar dans Help Center et File Exchange

Produits


Version

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by