Why my for loop does not work correctly?

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

Réponses (1)

Mohammad Sami
Mohammad Sami le 1 Avr 2020
Modifié(e) : Mohammad Sami le 1 Avr 2020
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 commentaires

Thank you,
The reason that I overwrited is that I wanted to have Class1_mon_avg_synop{1,i}.sum_rrr24(1) results in the first column of ALLMONTHS_class1_avg and Class1_mon_avg_grid{1,i}.sum_precip(1) results in the next column of it (ALLMONTHS_class1_avg).
After that:
Class1_mon_avg_synop{1,i}.sum_rrr24(2) results in the third column of ALLMONTHS_class1_avg and Class1_mon_avg_grid{1,i}.sum_precip(2) results in the next column (forth) of it.
And so on untill:
Class1_mon_avg_synop{1,i}.sum_rrr24(12) results in the eleventh column of ALLMONTHS_class1_avg and Class1_mon_avg_grid{1,i}.sum_precip(12) results in the next column (twelfth).
I run your code, thank you it's correct in cell size (12 x 24), But I don't know why so many cells are empty ([]), it should be filled by number.
For example when I run this lines of my code:
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
I have 2 columns like this:
Jan_class1_avg =
12×2 cell array
{[12.6337]} {[30.4854]}
{[21.9847]} {[42.8486]}
{[18.1653]} {[22.4421]}
{[13.5034]} {[23.2271]}
{[13.5493]} {[26.6221]}
{[23.8416]} {[28.8754]}
{[ 9.3836]} {[26.2704]}
{[18.1416]} {[28.6954]}
{[15.2941]} {[21.3786]}
{[15.2932]} {[23.1032]}
{[22.1976]} {[31.2746]}
{[14.1651]} {[14.0939]}
Now my purpose of writing loop is to have other couple columns of i = 2,3,4,5,...12 After this two columns.
Thank you.
dpb
dpb le 1 Avr 2020
Again, explain from the beginning just what it is you're trying to compute...without a clear description of the problem to be solved, a solution by trying to patch nonworking code is most inefficient use of time.
BN
BN le 1 Avr 2020
Modifié(e) : BN le 1 Avr 2020
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 le 2 Avr 2020
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...

Connectez-vous pour commenter.

Catégories

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

Produits

Version

R2018b

Tags

Question posée :

BN
le 31 Mar 2020

Commenté :

dpb
le 2 Avr 2020

Community Treasure Hunt

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

Start Hunting!

Translated by