Why my for loop does not work correctly?

1 次查看(过去 30 天)
BN
BN 2020-3-31
评论: dpb 2020-4-2
Hey all,
I had this code below:
for i = 1:numel(Class1_mon_avg_synop) %size of Class1_mon_avg_synop is 1 x 12 cell
ave(i) = Class1_mon_avg_synop{1,i}.sum_rrr24(1); % first row in sum_rrr24 is for January
Jan_class1_avg{i,1} = ave(i);
end
for i = 1:numel(Class1_mon_avg_grid) %size of Class1_mon_avg_grid is 1 x 12 cell
ave(i) = Class1_mon_avg_grid{1,i}.sum_precip(1);% first row in sum_precip is for January
Jan_class1_avg{i,2} = ave(i);
end
% Do same for next month
for i = 1:numel(Class1_mon_avg_synop)
ave(i) = mean(Class1_mon_avg_synop{1,i}.sum_rrr24(2)); % second row in sum_rrr24 is for February
Feb_class1_avg{i,1} = ave(i);
end
for i = 1:numel(Class1_mon_avg_grid)
ave(i) = mean(Class1_mon_avg_grid{1,i}.sum_precip(2)); % second row in sum_rrr24 is for February
Feb_class1_avg{i,2} = ave(i);
end
% do same for all other month
% ...
As I should calculate this for all 12 months then I wrote this for loop:
for i = 1:numel(Class1_mon_avg_synop)
ave(i) = Class1_mon_avg_synop{1,i}.sum_rrr24(i);
ave(i) = Class1_mon_avg_grid{1,i}.sum_precip(i);
ALLMONTHS_class1_avg{i,i} = ave(i);
ALLMONTHS_class1_avg{i,i+1} = ave(i);
end
But unfortunately, I got the wrong answer:
Because I think it must be 12 x 24 cell. 1st and 2nd column for January, 3rd, and 4th for February, etc...
Thank you

回答(1 个)

Mohammad Sami
Mohammad Sami 2020-4-1
编辑:Mohammad Sami 2020-4-1
for i = 1:numel(Class1_mon_avg_synop)
j = (i-1)*2;
ave(i) = Class1_mon_avg_synop{1,i}.sum_rrr24(i);
ALLMONTHS_class1_avg{i,j+1} = ave(i);
ave(i) = Class1_mon_avg_grid{1,i}.sum_precip(i); % this will overwrite ave(i)
ALLMONTHS_class1_avg{i,j+2} = ave(i);
end
note: your code was overwriting ave(i) value. i left it as it is as i don't know what was your intention.
  4 个评论
BN
BN 2020-4-1
编辑:BN 2020-4-1
Dear dpb,
Sorry if I was not clear,
I have 2 types of data sets (names are: synop and grid). Each one of them is 12 x 1 cell. In every arrays of both cells, there are 12 x 3 tables (12 because they are on a monthly basis, 3 because I have 3 columns (date, value, and rank number)). The purpose is to:
Aggregating every month's values in all 12 tables, in the first cell, and in the second cell, separately. Then put them in a cell or table in this way: column 1 and column 2 related to January (the first column of such a table or cell be January of synop and second column be January for grid). You can see the code that I wrote done this for me in January.
% I've replaced the names of my variables simpler than my question code,
% in order to make it clearer.
for i = 1:numel(synop) %size of both synop and grid are 1 x 12 cell
ave(i) = synop{1,i}.vlue(1); % first row in value column is for January
January_cell{i,1} = ave(i); % create a cell named January_cell and save the result in the first column of it.
end
% Similar process for grid cell
for i = 1:numel(grid) %size of both synop and grid are 1 x 12 cell
ave(i) = grid{1,i}.value(1);% first row in value column is for January
January_cell{i,2} = ave(i); % save result in 2nd column of January_cell
end
I would like to this for all other months, for example, put February of synop in third column and February of grid in the fourth column. As I have 12 months and having 2 cells so at the end the size of my cell would be 12 x 24.
Although the above code is working well for January, when I tried my best to write a loop it is not working well.
for i = 1:numel(synop) % or grid because both are 1 x12
ave(i) = synop{1,i}.value(i);
ave(i) = grid{1,i}.value(i);
ALLMONTHS_cell{i,i} = ave(i);
ALLMONTHS_cell{i,i+1} = ave(i);
end
I would be grateful if you can help me to fix my for loop.
I want to do this because I want to draw a bar chart including all month and represent the value of synop and grid in order to have a monthly comparison.
Yours faithfully
dpb
dpb 2020-4-2
Missed the response until now, sorry...
OK. I got the want; how about also attaching a .mat file with the data so can play without having to try to reconstruct something to test with. Looks likely the storage is overly complex for the problem but that's also hard to tell quickly just by reading...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by