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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Rik
Rik el 26 de Oct. de 2019
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 comentarios
Rik
Rik el 26 de Oct. de 2019
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by