# How to convert 3d monthly data into season

1 visualización (últimos 30 días)
Shakir Hussain el 1 de Mzo. de 2019
Comentada: Shakir Hussain el 5 de Mzo. de 2019
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.
##### 2 comentariosMostrar NingunoOcultar Ninguno
Guillaume el 1 de Mzo. de 2019
And how do you convert 3 months into a season? sum all the data? Take the mean, median, mode or something else?
Once we know that, performing the grouping is trivial.
Shakir Hussain el 2 de Mzo. de 2019
Dear Guillaume,I would like to make season with sum of monthly values.

Iniciar sesión para comentar.

Akira Agata el 3 de Mzo. de 2019
% 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]);
##### 6 comentariosMostrar 4 comentarios más antiguosOcultar 4 comentarios más antiguos
Guillaume el 4 de Mzo. de 2019
Note that to account for the fact that the 1st season is incomplete, and last one may be as well, you may need to create group slightly differently.
group = repelem((1:ceil((sz(3)+1)/3))', 3);
group = group(2:sz(3)+1);
Shakir Hussain el 5 de Mzo. de 2019
Thank you Akira and Guillaume for help.

Iniciar sesión para comentar.

### Más respuestas (1)

Guillaume el 4 de Mzo. de 2019
Editada: Guillaume el 4 de Mzo. de 2019
I don't see anything wrong with Akira's approach. It does what is asked (except that it has a wrong start of season).
However, I'd use a different approach. I'd simply reshape into an Mx3xS (where M is data points per month and S is number of seasons) and sum across the columns. You may need to add some extra months full of 0 (which don't affect the sum) to make sure that we've got 3 months for all seasons.
So, if the matrix starts in January and ends in December, we need to add one month of 0s at the start and 2 months of 0s at the end:
%data: a 3D matrix whose 3rd dimension is a month. It is assumed that first month is January
padded = cat(3, zeros(size(data, 1), size(data, 2), 'like', data), data); %prepend december of previous year to complete first season
padded = cat(3, padded, zeros(size(data, 1), size(data, 2), mod(-size(padded, 3), 3), 'like', data)); %append however months is necessary (0-1-2) to finish last season
seasonal = reshape(padded, size(data, 1) * size(data, 2), 3, []); %each column is a whole month, with 3 columns, each page is a season
seasonalsum = sum(seasonal, 2); %sum months within a season
seasonalsum = reshape(seasonalsum, size(data, 1), size(data, 2), []); %and reshape into original 3D shape
edit: Note that I'm padding with 0s since it doesn't affect the sum. If you were to calculate the mean or some other statistics, I'd pad with NaNs instead, and use the 'omitnan' option of the corresponding function. You could do that for the sum as well.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Spreadsheets en Help Center y File Exchange.

R2014a

### Community Treasure Hunt

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

Start Hunting!

Translated by