Daily 3d array to monthly- dealing with alternate days in a month and leap years - How to do it?

조회 수: 3 (최근 30일)
I have a 720x360x365 matrix (let's call it A) of daily precipitation for one year. 365 stands for days in a year. I need to write a code to convert these daily data to the monthly sum. If I start from January, I need to do mean (A,3) of the first 31 days, then the mean (A,3) of February, the next 28 or 29 days. Because the days alternate between 31 and 30 (and 28 or 29 for February), I don't know how to write a code to do this.
please help me I don't know how to do it.
thank you

채택된 답변

Rik
Rik 2019년 10월 26일
You don't have another option than just hardcoding the number of days in a month. You can do this in a fancy way using the date functions in Matlab, or just look at a calender. The fact that you have 365 days means that you are dealing with a non-leap year, so February only has 28 days.
There are probably simpler ways to solve it, but you can easily convert this 3D matrix to a cell array, apply the function you already suggested to each cell and then convert back to a 3D matrix.
A=rand(720,360,365);%generate example array
month_lengths=[31 28 31 30 31 30 31 31 30 31 30 31];%non-leap year
B=mat2cell(A,size(A,1),size(A,2),month_lengths);%divide into 1 cell per month
B=cellfun(@(x) mean(x,3),B,'UniformOutput',false);%find mean for each month
B=cell2mat(B);%convert back to 3D array
  댓글 수: 6
Rik
Rik 2019년 10월 26일
So you have a separate 3D array for each year? And you need a monthly average separate for each year? In the latter case it would be trivial to use something like this:
if size(A,3)==365
month_lengths=[31 28 31 30 31 30 31 31 30 31 30 31];%non-leap year
else
month_lengths=[31 29 31 30 31 30 31 31 30 31 30 31];%leap year
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by