how to calculating monthly average?

3 ビュー (過去 30 日間)
Lilya
Lilya 2016 年 5 月 8 日
コメント済み: Lilya 2016 年 5 月 13 日
hi all I just want to be sure about this small monthly averaging code
I=1;
for i=1: length(x)/30;
y(i)=mean(x(I:I+29));
L(i)=min(x(I:I+29));
U(i)=max(x(I:I+29));
I=I+29;
end
where x is data matrix (365 day)
thank you
  4 件のコメント
Star Strider
Star Strider 2016 年 5 月 8 日
Well, some of the data, but not what we need to do what you want with it.
Without knowing the times (at least months-days-years, in some format) at which these are collected, it’s not possible to separate them by month.
Lilya
Lilya 2016 年 5 月 8 日
Thank you for help

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

採用された回答

Image Analyst
Image Analyst 2016 年 5 月 8 日
Try this:
dailyValues = rand(1,365); % Sample data
daysInMonths = [31,28,31,30,31,30,31,31,30,31,30,31]
monthEnds = [0, cumsum(daysInMonths)]
for m = 1 : length(monthEnds)-1
firstDay = monthEnds(m)+1;
lastDay = monthEnds(m+1);
fprintf('For month #%d, the first day = %d and the last day = %d\n',...
m, firstDay, lastDay);
monthlyAverage(m) = mean(dailyValues(firstDay:lastDay));
end
% Echo to command window.
monthlyAverage
Of course you need to take leap years into account, which I didn't do.
  7 件のコメント
Image Analyst
Image Analyst 2016 年 5 月 13 日
JanuaryDailyAmounts = dailyValues(1:31);
FebruaryDailyAmounts = dailyValues(32:59);
MarchDailyAmounts = dailyValues(60:90);
and so on for months ending at rows 120 151 181 212 243 273 304 334 365.
You will then have 12 matrices.
Lilya
Lilya 2016 年 5 月 13 日
I appreciate your help. Thank you.

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2016 年 5 月 12 日
Find the month number (either directly from the file or using some of the tools associated with datetime objects then use the month numbers as the subscripts in a call to accumarray, specifying @mean as the accumulation function.
numRows = 30;
X = [randi(12, numRows, 1), randi(100, numRows, 1)] % Column 1 is month, column 2 is data
Y = accumarray(X(:, 1), X(:, 2), [], @mean)
  1 件のコメント
Lilya
Lilya 2016 年 5 月 13 日
I appreciate your help

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by