finding rows which correspond to date

5 ビュー (過去 30 日間)
Dom Smith
Dom Smith 2017 年 5 月 16 日
コメント済み: Dom Smith 2017 年 5 月 17 日
I have a table of data with two columns, one with monthly averaged data values and the other with the corresponding month and year in datetime 'MMM yyyy' format, e.g.
Jan 2004 26.3
Mar 2004 27.2
Jun 2004 35.5
Jan 2006 45.1
Mar 2007 32.2
How can I select each row beginning with a specific month eg. March (for each year), to then average these and get a representative average for that month over the full time span?
Many thanks!
  2 件のコメント
Dom Smith
Dom Smith 2017 年 5 月 16 日
編集済み: Dom Smith 2017 年 5 月 16 日
The 'table' format doesn't support 'find', so I will keep the two columns as separate files. In which case I need to find the row dependant on month, and then find the corresponding value matching that row within the separate column of numbers
Peter Perkins
Peter Perkins 2017 年 5 月 16 日
Dom, you're right that you can't call find on a table, but you can call find on a variable in a table.
find(month(t.Date) == 3)
for example. But usually, you don't want to call find anyway (leave it as a logical)
t(month(t.Date) == 3,:)
And in this case, grouped calculations are already provided as part of varfun.

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

採用された回答

Peter Perkins
Peter Perkins 2017 年 5 月 16 日
>> Date = datetime({'Jan 2004';'Mar 2004';'Jun 2004';'Jan 2006';'Mar 2007'},'Format','MMM yyyy');
>> Value = [26.3;27.2;35.5;45.1;32.2];
>> t = table(Date,Value)
t =
5×2 table
Date Value
________ _____
Jan 2004 26.3
Mar 2004 27.2
Jun 2004 35.5
Jan 2006 45.1
Mar 2007 32.2
>> t.Month = month(t.Date,'shortname')
t =
5×3 table
Date Value Month
________ _____ _____
Jan 2004 26.3 'Jan'
Mar 2004 27.2 'Mar'
Jun 2004 35.5 'Jun'
Jan 2006 45.1 'Jan'
Mar 2007 32.2 'Mar'
>> monthly = varfun(@mean,t,'GroupingVariable','Month','InputVariable','Value')
monthly =
3×3 table
Month GroupCount mean_Value
_____ __________ __________
'Jan' 2 35.7
'Jun' 1 35.5
'Mar' 2 29.7
  1 件のコメント
Dom Smith
Dom Smith 2017 年 5 月 17 日
Legend, thank you kindly!

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

その他の回答 (0 件)

カテゴリ

Help Center および 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