hi,
i have a 100x11 char array with dates (e.g. '03-Jan-2006'). how can i create an array with the number of days in each unique month that appear in my date array. (e.g. if i have two days in january 2006, then i want it to return 2). any help would be appreciated. thx in advance.
DB

 採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 10 月 22 日
編集済み: Andrei Bobrov 2017 年 10 月 22 日

0 投票

Here A - your array "100x11 char array with dates (e.g. '03-Jan-2006')"
z = datetime(A);
T = table(datetime(z,'F','MMM-uuuu'),eomday(year(z),month(z)),'v',{'date','days'});

4 件のコメント

dan berkowitz
dan berkowitz 2017 年 10 月 23 日
close, but not quite. this give the number of days in each calendar month. what i'm trying to get is the number of days for each month in my datetime vector (i.e. if i have two days in january 2006, then i want it to return 2). ideally, i'd like a column with each unique month then number of days in my datetime vector that are in that month, e.g. [january 2006, 2].
Andrei Bobrov
Andrei Bobrov 2017 年 10 月 23 日
編集済み: Andrei Bobrov 2017 年 10 月 23 日
[y,m] = ymd(z);
[a,~,c] = unique([y,m],'rows');
T_out = table(datetime(a(:,1),a(:,2),1,'f','MMM-uuuu'),...
accumarray(c,1),'v',{'date','days'});
dan berkowitz
dan berkowitz 2017 年 10 月 23 日
Thank you! I didn't know about the ymd function! Appreciate your help.
Andrei Bobrov
Andrei Bobrov 2017 年 10 月 23 日
[y,m] = ymd(z);
or
[y,m] = datevec(z);

サインインしてコメントする。

その他の回答 (1 件)

KL
KL 2017 年 10 月 22 日
編集済み: KL 2017 年 10 月 22 日

0 投票

It's better to change your char array into datetime vector
dt_array = datetime(char_array,'InputFormat','dd-MMM-yyyy')
and then you can use
numOfDays = nnz(dt_array.Month==your_month)

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by