nested loop will not enter second iteration

8 vues (au cours des 30 derniers jours)
Jennifer Walker
Jennifer Walker le 1 Juil 2017
Commenté : Jennifer Walker le 1 Juil 2017
Hi,
Below is my code. For each month of each year I am trying to get the average and std for a set of values. The first iteration works for the first year. But then it does not continue. Can someone tell me where the problem is.
[M,ia,ic] = unique(CO2_FFD(:,2)); %find unique years, M = unique year, ia = row with unique year
m=[1;2;3;4;5;6;7;8;9;10;11;12];%set an array for 12 months
CO2_YM_averages = zeros(length(M)*12,2); %array for averages & std's, size of year*12months
for i=1:length(M)
yrows = find(M(i)==CO2_FFD(:,2)); %find rows where year is equal to M(i)
for j=1:length(m)
mrows=find(CO2_FFD(yrows,3)== m(j));%find rows where month = m(i)
CO2_YM_averages(j,1) = mean(CO2_FFD(mrows,8)); %average data for each month
CO2_YM_averages(j,2) = std(CO2_FFD(mrows,8)); %std data for each month
end
end
  4 commentaires
Jennifer Walker
Jennifer Walker le 1 Juil 2017
I can see that i is not incrementing when the code stops >> i
i =
1
Jennifer Walker
Jennifer Walker le 1 Juil 2017
here is some sample data:
CO2_FFD = [0 2000 1 1 0 0 0 340 ; 0 2000 1 2 0 0 0 380 ; 0 2000 2 3 0 0 0 280 ; 0 2000 2 4 0 0 0 450 ; 0 2000 3 5 0 0 0 330 ; 0 2000 3 6 0 0 0 450 ; 0 2000 4 7 0 0 0 380 ; 0 2000 4 8 0 0 0 290 ; 0 2000 5 9 0 0 0 340 ; 0 2000 5 10 0 0 0 380 ; 0 2000 6 11 0 0 0 280 ; 0 2000 6 12 0 0 0 450 ; 0 2000 7 13 0 0 0 330 ; 0 2000 7 14 0 0 0 450 ; 0 2000 8 15 0 0 0 380 ; 0 2000 8 16 0 0 0 290 ; 0 2000 9 17 0 0 0 340 ; 0 2000 9 18 0 0 0 380 ; 0 2000 10 19 0 0 0 280 ; 0 2000 10 20 0 0 0 450 ; 0 2000 11 21 0 0 0 330 ; 0 2000 11 22 0 0 0 450 ; 0 2000 12 23 0 0 0 380 ; 0 2000 12 24 0 0 0 290 ; 0 2001 1 25 0 0 0 340 ; 0 2001 1 26 0 0 0 380 ; 0 2001 2 27 0 0 0 280 ; 0 2001 2 28 0 0 0 450 ; 0 2001 3 29 0 0 0 330 ; 0 2001 3 30 0 0 0 450 ; 0 2001 4 1 0 0 0 380 ; 0 2001 4 2 0 0 0 290 ; 0 2001 5 3 0 0 0 340 ; 0 2001 5 4 0 0 0 380 ; 0 2001 6 5 0 0 0 280 ; 0 2001 6 6 0 0 0 450 ; 0 2001 7 7 0 0 0 330 ; 0 2001 7 8 0 0 0 450 ; 0 2001 8 9 0 0 0 380 ; 0 2001 8 10 0 0 0 290 ; 0 2001 9 11 0 0 0 340 ; 0 2001 9 12 0 0 0 380 ; 0 2001 10 13 0 0 0 280 ; 0 2001 10 14 0 0 0 450 ; 0 2001 11 15 0 0 0 330 ; 0 2001 11 16 0 0 0 450 ; 0 2001 12 17 0 0 0 380 ; 0 2001 12 18 0 0 0 290 ; 0 2002 1 19 0 0 0 340 ; 0 2002 1 20 0 0 0 380 ; 0 2002 2 21 0 0 0 280 ; 0 2002 2 22 0 0 0 450 ; 0 2002 3 23 0 0 0 330 ; 0 2002 3 24 0 0 0 450 ; 0 2002 4 25 0 0 0 380 ; 0 2002 4 26 0 0 0 290 ; 0 2002 5 27 0 0 0 340 ; 0 2002 5 28 0 0 0 380 ; 0 2002 6 29 0 0 0 280 ; 0 2002 6 30 0 0 0 450 ; 0 2002 7 31 0 0 0 330 ; 0 2002 7 32 0 0 0 450 ; 0 2002 8 33 0 0 0 380 ; 0 2002 8 34 0 0 0 290 ; 0 2002 9 35 0 0 0 340 ; 0 2002 9 36 0 0 0 380 ; 0 2002 10 37 0 0 0 280 ; 0 2002 10 38 0 0 0 450 ; 0 2002 11 39 0 0 0 330 ; 0 2002 11 40 0 0 0 450 ; 0 2002 12 41 0 0 0 380 ; 0 2002 12 42 0 0 0 290 ]

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 1 Juil 2017
I get 3 tierations for i. Here is my code:
% here is some sample data:
CO2_FFD = [0 2000 1 1 0 0 0 340 ;
0 2000 1 2 0 0 0 380 ;
0 2000 2 3 0 0 0 280 ;
0 2000 2 4 0 0 0 450 ;
0 2000 3 5 0 0 0 330 ;
0 2000 3 6 0 0 0 450 ;
0 2000 4 7 0 0 0 380 ;
0 2000 4 8 0 0 0 290 ;
0 2000 5 9 0 0 0 340 ;
0 2000 5 10 0 0 0 380 ;
0 2000 6 11 0 0 0 280 ;
0 2000 6 12 0 0 0 450 ;
0 2000 7 13 0 0 0 330 ;
0 2000 7 14 0 0 0 450 ;
0 2000 8 15 0 0 0 380 ;
0 2000 8 16 0 0 0 290 ;
0 2000 9 17 0 0 0 340 ;
0 2000 9 18 0 0 0 380 ;
0 2000 10 19 0 0 0 280 ;
0 2000 10 20 0 0 0 450 ;
0 2000 11 21 0 0 0 330 ;
0 2000 11 22 0 0 0 450 ;
0 2000 12 23 0 0 0 380 ;
0 2000 12 24 0 0 0 290 ;
0 2001 1 25 0 0 0 340 ;
0 2001 1 26 0 0 0 380 ;
0 2001 2 27 0 0 0 280 ;
0 2001 2 28 0 0 0 450 ;
0 2001 3 29 0 0 0 330 ;
0 2001 3 30 0 0 0 450 ;
0 2001 4 1 0 0 0 380 ;
0 2001 4 2 0 0 0 290 ;
0 2001 5 3 0 0 0 340 ;
0 2001 5 4 0 0 0 380 ;
0 2001 6 5 0 0 0 280 ;
0 2001 6 6 0 0 0 450 ;
0 2001 7 7 0 0 0 330 ;
0 2001 7 8 0 0 0 450 ;
0 2001 8 9 0 0 0 380 ;
0 2001 8 10 0 0 0 290 ;
0 2001 9 11 0 0 0 340 ;
0 2001 9 12 0 0 0 380 ;
0 2001 10 13 0 0 0 280 ;
0 2001 10 14 0 0 0 450 ;
0 2001 11 15 0 0 0 330 ;
0 2001 11 16 0 0 0 450 ;
0 2001 12 17 0 0 0 380 ;
0 2001 12 18 0 0 0 290 ;
0 2002 1 19 0 0 0 340 ;
0 2002 1 20 0 0 0 380 ;
0 2002 2 21 0 0 0 280 ;
0 2002 2 22 0 0 0 450 ;
0 2002 3 23 0 0 0 330 ;
0 2002 3 24 0 0 0 450 ;
0 2002 4 25 0 0 0 380 ;
0 2002 4 26 0 0 0 290 ;
0 2002 5 27 0 0 0 340 ;
0 2002 5 28 0 0 0 380 ;
0 2002 6 29 0 0 0 280 ;
0 2002 6 30 0 0 0 450 ;
0 2002 7 31 0 0 0 330 ;
0 2002 7 32 0 0 0 450 ;
0 2002 8 33 0 0 0 380 ;
0 2002 8 34 0 0 0 290 ;
0 2002 9 35 0 0 0 340 ;
0 2002 9 36 0 0 0 380 ;
0 2002 10 37 0 0 0 280 ;
0 2002 10 38 0 0 0 450 ;
0 2002 11 39 0 0 0 330 ;
0 2002 11 40 0 0 0 450 ;
0 2002 12 41 0 0 0 380 ;
0 2002 12 42 0 0 0 290 ]
[M,ia,ic] = unique(CO2_FFD(:,2)); %find unique years, M = unique year, ia = row with unique year
m=[1;2;3;4;5;6;7;8;9;10;11;12];%set an array for 12 months
CO2_YM_averages = zeros(length(M)*12,2); %array for averages & std's, size of year*12months
for i=1:length(M)
fprintf('i = %d\n', i);
yrows = find(M(i)==CO2_FFD(:,2)); %find rows where year is equal to M(i)
for j=1:length(m)
fprintf(' j = %d\n', j);
mrows=find(CO2_FFD(yrows,3)== m(j));%find rows where month = m(i)
CO2_YM_averages(j,1) = mean(CO2_FFD(mrows,8)); %average data for each month
CO2_YM_averages(j,2) = std(CO2_FFD(mrows,8)); %std data for each month
end
end
and here is the results:
i = 1
j = 1
j = 2
j = 3
j = 4
j = 5
j = 6
j = 7
j = 8
j = 9
j = 10
j = 11
j = 12
i = 2
j = 1
j = 2
j = 3
j = 4
j = 5
j = 6
j = 7
j = 8
j = 9
j = 10
j = 11
j = 12
i = 3
j = 1
j = 2
j = 3
j = 4
j = 5
j = 6
j = 7
j = 8
j = 9
j = 10
j = 11
j = 12
Why do you say there is only one iteration for i???
  3 commentaires
Jennifer Walker
Jennifer Walker le 1 Juil 2017
Oh I see my problem now, I was rewriting over my rows by calling j in every loop. I fixed it using this:
[M,ia,ic] = unique(CO2_FFD(:,2)); %find unique years/month, M = unique year/month, ia = row with unique year/month m=[1;2;3;4;5;6;7;8;9;10;11;12];%array for 12 months CO2_YM_averages = zeros(length(M)*12,2); %array for averages, size of year/month for i=1:length(M) yrows = find(M(i)==CO2_FFD(:,2)); %find rows where year is equal to M(i) for j=1:length(m) %j=j+1; mrows=find(CO2_FFD(yrows,3)== m(j));%find rows where month = m(i) r = (i-1)*12+j; CO2_YM_averages(r,1) = mean(CO2_FFD(mrows,8)); %average data for each day CO2_YM_averages(r,2) = std(CO2_FFD(mrows,8)); end end
Jennifer Walker
Jennifer Walker le 1 Juil 2017
Thank you so much for your help! Without the print function I would have never figured it out. Thats a great trick!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by