Calculate mean value between 2 years

2 ビュー (過去 30 日間)
Anh Duong
Anh Duong 2022 年 3 月 30 日
コメント済み: Anh Duong 2022 年 3 月 31 日
I have a set of data from Jan 2000 to 2011, and I want to calculate mean value of data from Nov of one year to Apr of the next year. How can I do it?

採用された回答

Simon Chan
Simon Chan 2022 年 3 月 30 日
編集済み: Simon Chan 2022 年 3 月 30 日
Not sure what is the format of your data. Use function isbetween and below shows an example that you can implement it into your code.
TimeStamps = datetime([2000 11 4; 2001 3 2; 2001 8 15; 2001 10 10;...
2001 12 14; 2002 1 31; 2002 3 25;...
2002 4 29; 2002 12 21; 2003 3 18]);
Profit = [2032 3071 1185 2587 1998 2899 3112 909 2619 3085]';
TT = timetable(TimeStamps,Profit);
tlower = datetime(2000,11,1)+calyears(0:11);
thigher = datetime(2001,4,30)+calyears(0:11);
meanProfit = arrayfun(@(x,y) mean(TT.Profit(isbetween(TT.TimeStamps,x,y))),tlower,thigher)
meanProfit = 1×12
1.0e+03 * 2.5515 2.2295 2.8520 NaN NaN NaN NaN NaN NaN NaN NaN NaN
  3 件のコメント
Simon Chan
Simon Chan 2022 年 3 月 31 日
Do you want the following output?
Or do you want to use function timerange?
TimeStamps = datetime([2000 11 4; 2001 3 2; 2001 8 15; 2001 10 10;...
2001 12 14; 2002 1 31; 2002 3 25;...
2002 4 29; 2002 12 21; 2003 3 18]);
Profit = [2032 3071 1185 2587 1998 2899 3112 909 2619 3085]';
TT = timetable(TimeStamps,Profit);
tlower = datetime(2000,11,1)+calyears(0:11);
thigher = datetime(2001,4,30)+calyears(0:11);
meanProfit = arrayfun(@(x,y) mean(TT.Profit(isbetween(TT.TimeStamps,x,y))),tlower,thigher);
table(tlower',thigher',meanProfit','VariableName',{'Period Start','Period End','Mean'})
ans = 12×3 table
Period Start Period End Mean ____________ ___________ ______ 01-Nov-2000 30-Apr-2001 2551.5 01-Nov-2001 30-Apr-2002 2229.5 01-Nov-2002 30-Apr-2003 2852 01-Nov-2003 30-Apr-2004 NaN 01-Nov-2004 30-Apr-2005 NaN 01-Nov-2005 30-Apr-2006 NaN 01-Nov-2006 30-Apr-2007 NaN 01-Nov-2007 30-Apr-2008 NaN 01-Nov-2008 30-Apr-2009 NaN 01-Nov-2009 30-Apr-2010 NaN 01-Nov-2010 30-Apr-2011 NaN 01-Nov-2011 30-Apr-2012 NaN
Anh Duong
Anh Duong 2022 年 3 月 31 日
I want to express the time as a column to plot a mean-time graph based on the table, should i use the aforementioned method or timerange function?

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

その他の回答 (1 件)

Sam Chak
Sam Chak 2022 年 3 月 30 日
I think the M = mean(A) function will do the job. Give it a try.

カテゴリ

Help Center および File ExchangeTime Series Events についてさらに検索

タグ

製品


リリース

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by