Daily to monthly values in a loop
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Andrei Bobrov
le 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 commentaires
Voir également
Catégories
En savoir plus sur Language Fundamentals dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!