Daily to monthly values in a loop
4 views (last 30 days)
Show older comments
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 :)
0 Comments
Answers (1)
Andrei Bobrov
on 28 Oct 2011
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 Comments
See Also
Categories
Find more on Language Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!