How to write a LOOP for this case?

Hey all
I have 34 variable in my workplace like this:
precip_1982_daily
precip_1983_daily
precip_1984_daily
precip_1985_daily
...
precip_2015_daily
I want to use this if code below:
if size(precip_1982_daily,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
precip_1982_monthly=mat2cell(precip_1982_daily,size(precip_1982_daily,1),size(precip_1982_daily,2),month_lengths);%divide into 1 cell per month
precip_1982_monthly=cellfun(@(x) nansum(x,3),precip_1982_monthly,'UniformOutput',false);%find mean for each month
precip_1982_monthly=cell2mat(precip_1982_monthly);%convert back to 3D array

 Respuesta aceptada

Guillaume
Guillaume el 28 de Oct. de 2019
So again, numbered variables and field names are a bad idea. (see here and here) for the related questions). The whole thing would be simpler if the original cell array was used.
If the original cell array had been kept, the code over the 3 questions could be simplified to:
%using original cell array precips
precip_monthly = cell(size(precips)); %preallocate result cell array
for idx = 1:numel(precips)
if size(precips{idx}, 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
p_month =mat2cell(precips{idx}, size(precips{idx}, 1), size(precips{idx}, 2), month_lengths);%divide into 1 cell per month
p_month = cellfun(@(x) sum(x, 3, 'omitnan'), p_month, 'UniformOutput', false); %find mean for each month !!THIS CALCULATE THE SUM!!
precip_monthly{idx} = cell2mat(p_month);%convert back to 3D array
end
Note that you have written a comment that says find mean for each month but you use the sum function. So either the code or the comment is wrong.

1 comentario

BN
BN el 28 de Oct. de 2019
I greatly appreciate your responses.
special thanks to both of you :)

Iniciar sesión para comentar.

Más respuestas (1)

Shubham Gupta
Shubham Gupta el 28 de Oct. de 2019
Editada: Shubham Gupta el 28 de Oct. de 2019
I think this is the continuation of this question.
So, once you create struture as mentioned in the above answer, you can use:
for i = 1:length(precip)
precip_daily = precip.(['y',num2str(i+1981)]); % Access data of specific year
if size(precip_daily,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
precip_monthly=mat2cell(precip_daily,size(precip_daily,1),size(precip_daily,2),month_lengths);%divide into 1 cell per month
precip_monthly=cellfun(@(x) nansum(x,3),precip_monthly,'UniformOutput',false);%find mean for each month
precip_monthly=cell2mat(precip_monthly);%convert back to 3D array
precp_monthly.(['y',num2str(i+1981)]) = precip_monthly; % Create precip_monthly structure
end

4 comentarios

BN
BN el 28 de Oct. de 2019
Editada: BN el 28 de Oct. de 2019
Dear Shubham,
Thank you. yes you right it's the continuation of this question. when I try to use this code above, the following red text appeared in the command window:
Unable to perform assignment because dot indexing is not supported for
variables of this type.
do you have an idea?
Thank you again
BN
BN el 28 de Oct. de 2019
Thank you again the error was fixed. but now it seems that the loop not working because the result pf precp_monthly is only for y 1982. I attached the picture of my workplace.precp.JPG
Oh, I am sorry. Can you try replace the for loop statement by :
for i = 1:length(precips) % "precips" instead of "precip"
BN
BN el 28 de Oct. de 2019
Thank you, it's perfect code.
I really appreciate your help. You've just made my day :)

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

BN
el 28 de Oct. de 2019

Comentada:

BN
el 28 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by