nested loop will not enter second iteration
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jennifer Walker
am 1 Jul. 2017
Kommentiert: Jennifer Walker
am 1 Jul. 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 Kommentare
Akzeptierte Antwort
Image Analyst
am 1 Jul. 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!