필터 지우기
필터 지우기

Daily to monthly values in a loop

조회 수: 2 (최근 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
Andrei Bobrov
Andrei Bobrov 2011년 10월 28일
see corrected

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by