Record every 12 values in a loop

I have monthly data that I would like to group by year, so every 12 values.
I have created a loop:
for ii = 1:length(monthly)
years = monthly(1:12:end);
chl_years{ii} = years;
end
This records the 12th value for each year, when instead I need values 1-12 as one cell array, 13-24 as another, and so on.
I'm not sure what to change in order to do this. Thank you.

Réponses (1)

Rik
Rik le 16 Juin 2021
Using a cell as an intermediate step is generally not efficient, but it tend to be easy to work with.
With that in mind: you can use mat2cell to split an array into groups of 12.
data=1:10;
k=2;
mat2cell(data,1,k*ones(1,numel(data)/k))
ans = 1×5 cell array
{[1 2]} {[3 4]} {[5 6]} {[7 8]} {[9 10]}

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Question posée :

le 16 Juin 2021

Réponse apportée :

Rik
le 16 Juin 2021

Community Treasure Hunt

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

Start Hunting!

Translated by