フィルターのクリア

How to find (i) unique datetimes and (ii) count their occurences?

19 ビュー (過去 30 日間)
Sim
Sim 2023 年 6 月 14 日
回答済み: Sim 2023 年 6 月 14 日
How to find (i) unique datetimes and (ii) count their occurences?
% Input
a = [{'22-Jun-2023 09:00:00'}
{'23-Jun-2023 19:00:00'}
{'24-Jun-2023 16:00:00'}
{'24-Jun-2023 11:00:00'}
{'19-Jun-2023 16:00:00'}
{'20-Jun-2023 10:00:00'}
{'21-Jun-2023 10:00:00'}
{'22-Jun-2023 09:00:00'}
{'23-Jun-2023 14:00:00'}
{'19-Jun-2023 17:00:00'}
{'20-Jun-2023 11:00:00'}
{'21-Jun-2023 17:00:00'}
{'22-Jun-2023 15:00:00'}
{'23-Jun-2023 06:00:00'}
{'24-Jun-2023 11:00:00'}
{'25-Jun-2023 19:00:00'}
{'20-Jun-2023 11:00:00'}
{'21-Jun-2023 09:00:00'}
{'23-Jun-2023 12:00:00'}
{'25-Jun-2023 17:00:00'}
{'23-Jun-2023 12:00:00'}
{'22-Jun-2023 07:00:00'}];
% My attempt:
% (1) Get datetime from cells
b = datetime(a,'InputFormat','dd-MMM-yyyy HH:mm:ss');
% (2) Get unique dates
[~, inds] = unique(datestr(b, 'yyyymmdd'), 'rows', 'stable');
uniqueDates = sort(b(inds))
uniqueDates = 7×1 datetime array
19-Jun-2023 16:00:00 20-Jun-2023 10:00:00 21-Jun-2023 10:00:00 22-Jun-2023 09:00:00 23-Jun-2023 19:00:00 24-Jun-2023 16:00:00 25-Jun-2023 19:00:00
% (3) Count occurences for each date
occurences = groupsummary(table(b),1)
occurences = 18×2 table
b GroupCount ____________________ __________ 19-Jun-2023 16:00:00 1 19-Jun-2023 17:00:00 1 20-Jun-2023 10:00:00 1 20-Jun-2023 11:00:00 2 21-Jun-2023 09:00:00 1 21-Jun-2023 10:00:00 1 21-Jun-2023 17:00:00 1 22-Jun-2023 07:00:00 1 22-Jun-2023 09:00:00 2 22-Jun-2023 15:00:00 1 23-Jun-2023 06:00:00 1 23-Jun-2023 12:00:00 2 23-Jun-2023 14:00:00 1 23-Jun-2023 19:00:00 1 24-Jun-2023 11:00:00 2 24-Jun-2023 16:00:00 1
% Desired Output
uniqueDates =
19-Jun-2023
20-Jun-2023
21-Jun-2023
22-Jun-2023
23-Jun-2023
24-Jun-2023
25-Jun-2023
occurences =
19-Jun-2023 2
20-Jun-2023 3
21-Jun-2023 3
22-Jun-2023 4
23-Jun-2023 5
24-Jun-2023 3
25-Jun-2023 2

回答 (1 件)

Sim
Sim 2023 年 6 月 14 日
I might have found a solution/workaround, but if there are more compact, correct and robust methods I will accept them!
a = [{'22-Jun-2023 09:00:00'}
{'23-Jun-2023 19:00:00'}
{'24-Jun-2023 16:00:00'}
{'24-Jun-2023 11:00:00'}
{'19-Jun-2023 16:00:00'}
{'20-Jun-2023 10:00:00'}
{'21-Jun-2023 10:00:00'}
{'22-Jun-2023 09:00:00'}
{'23-Jun-2023 14:00:00'}
{'19-Jun-2023 17:00:00'}
{'20-Jun-2023 11:00:00'}
{'21-Jun-2023 17:00:00'}
{'22-Jun-2023 15:00:00'}
{'23-Jun-2023 06:00:00'}
{'24-Jun-2023 11:00:00'}
{'25-Jun-2023 19:00:00'}
{'20-Jun-2023 11:00:00'}
{'21-Jun-2023 09:00:00'}
{'23-Jun-2023 12:00:00'}
{'25-Jun-2023 17:00:00'}
{'23-Jun-2023 12:00:00'}
{'22-Jun-2023 07:00:00'}];
% Get datetime from cells
b = datetime(a,'InputFormat','dd-MMM-yyyy HH:mm:ss','Format','dd-MMM-yyyy');
c = dateshift(b, 'start', 'day');
% Get unique dates
[~, inds] = unique(datestr(c, 'yyyymmdd'), 'rows', 'stable');
uniqueDates = sort(c(inds))
uniqueDates = 7×1 datetime array
19-Jun-2023 20-Jun-2023 21-Jun-2023 22-Jun-2023 23-Jun-2023 24-Jun-2023 25-Jun-2023
% Count occurences for each date
d = groupsummary(table(c),1)
d = 7×2 table
c GroupCount ___________ __________ 19-Jun-2023 2 20-Jun-2023 3 21-Jun-2023 3 22-Jun-2023 4 23-Jun-2023 5 24-Jun-2023 3 25-Jun-2023 2

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by