how to calculate common dates from two date arrays?
古いコメントを表示
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 件のコメント
load('dates.mat')
whos
dates1
y = dates1.dStrt;
class(y)
y(1)
unique(y)
z= dates1.dStp;
class(z)
z(1)
unique(z)
Note - The dates1 data you have attached contains numeric double values (with all the values being same), not datenums - as you can see above.
I'm not sure how to interpret this.
Sven Larsen
2025 年 10 月 17 日
編集済み: Sven Larsen
2025 年 10 月 17 日
datenum(now)
This is tthe same range as the 7.3933e+05 shown above, so the values are indeed datenum. The values are not datetime objects.
"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
dates1
y = dates1.dStrt;
size(y)
z = vertcat(dates1.dStrt);
size(z)
"same algorithm works finding common number ranges also :)"
Similar logic will work. Same algorithm might not.
Please check Star Strider's answer below.
dpb
2025 年 10 月 18 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!