MATLAB: How to convert 3d monthly data into season

3dconvertMATLAB

Example of my data set is 56*42*360 (30 years) and each page represent a month for example 56*42*1 mean jan 1980 and 56*42*13 means jan 1981. I want to get only seasonal value not the number of months in a season, we have to define season as spring(mar, apr ,may), summer(jun,jul,aug) and so on.
I found a relevant solution here but it gives only number of months of a single season. I need seasons in staright as spring of 1980…….to autumn of 2010.
Expected outcome is something like 56*42*119.

Best Answer

How about the following?
% Sample data
Data = rand(56,42,360);
% Reshape the data so that each row shows monthly data (42*56)
Data = permute(Data,[3 2 1]);
Data = reshape(Data,360,42*56);
% Calculate sum for evely 3 months
group = repelem((1:120)',3);
Data2 = splitapply(@sum,Data, group);
% Reshape the data to the original format
Data2 = reshape(Data2,120,42,56);
Data2 = permute(Data2,[3 2 1]);