Daily to monthly values in a loop

3 次查看(过去 30 天)
Anna
Anna 2011-10-28
Hello,
I have 50 columns of monthly data for the years 1951-2000. However, the monthly data is in fact the daily average for that month and now I would like to convert it into monthly total by multiplying the daily average by the number of days in each month. So I want to multiple every January in the time series by 31, February by 28, March by 31 and so on.
I would like to write a loop that processes all months and all columns but am not quite sure how I can tell matlab to multiple each month with the correct number of days. Any help would be greatly appreciated :)

回答(1 个)

Andrei Bobrov
Andrei Bobrov 2011-10-28
yourdata - your array (12 x 50)
out = yourdata.*bsxfun(@(x,y)eomday(x,y),1951:2000,(1:12)')
corrected
yourdata - your array (600 x 50)
dsmhyr = bsxfun(@(x,y)eomday(x,y),1951:2000,(1:12)');
out = bsxfun(@times,dsmhyr(:),yourdata);
more variant
y=1951:2000;
N = numel(y)*12;
daysinmonth = 31*ones(N,1);
daysinmonth(bsxfun(@plus,[2:2:6,9:2:12]',0:12:N-1))=30;
n = 2:12:N;
daysinmonth(n(~rem(y,400)|(rem(y,100)&~rem(y,4))))=29;
out = bsxfun(@times,daysinmonth,yourdata);
  4 个评论
Anna
Anna 2011-10-28
column: monthly runoff data for 50 different river basins (so each column represents a different basin)
row: time series of data
row 1: runoff in Jan 1951
row 2: runoff in Feb 1951
row 3: runoff in Mar 1951
...
...
row 600: runoff in Dec 2000

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by