How to put the second "for" loop inside the first one?
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I need to combine two "for" loops.
I actually need the second loop (c and t arrays) to pass through all the results from the first one (a and b). This means a total of 4 results, because the permutations of a and b would be 4.
I have combined them, but it does not work. The output is only 2 results, while they should be 4.
First loop:
close all; clc; clear all;
a = [1 2 3 4 5];
b = [1 2 3 3 5];
for i=1:5;
for j=1:5;
for k = ((i-1)*5)+ j;
line1 = ['set m1 ' num2str(c(i)) ';'];
line2 = ['set m2 ' num2str(s(j)) ';'];
line3 = ['set case ' num2str(t(k)) ';'];
fid=fopen('MatParam8.0.tcl','w');
fprintf(fid,'%s\n',line1);
fprintf(fid,'%s\n',line2);
fprintf(fid,'%s\n',line3);
fid=fclose('all');
!OpenSees Model.tcl
end
end
end
Second loop:
fin = 'result_all.txt';
s = dlmread(fin,'');
c=[1 2 3 4 5];
t=[1 2 3 4 5];
for j = 1:400;
fidP = fopen('Pulse.acc','w+');
fidC= fopen('Case.tcl','w+');
fprintf(fidP, '%d\n%d\n%d',c(j), s(j), t(j));
fprintf(fidC, 'set case %d',j);
fclose(fidP);
fclose(fidC);
!OpenSees Model.tcl
end
The combination:
close all; clc; clear all;
a = [1 2];
b = [1 2];
for i=1:2;
for j=1:2;
for k = ((i-1)*2)+ j;
line1 = ['set m1 ' num2str(a(i)) ';'];
line2 = ['set m2 ' num2str(b(j)) ';'];
line3 = ['set case ' num2str((k)) ';'];
fid=fopen('MatParam8.0.tcl','w');
fprintf(fid,'%s\n',line1);
fprintf(fid,'%s\n',line2);
fprintf(fid,'%s\n',line3);
fid=fclose('all');
fin = 'result_all.txt';
s = dlmread(fin,'');
c=[1 2];
t=[1 2];
for m = 1:4;
fidP = fopen('Pulse.acc','w+');
fidC= fopen('Case.tcl','w+');
fprintf(fidP, '%d\n%d\n%d',c(m), s(m), t(m));
fprintf(fidC, 'set case %d',j);
fclose(fidP);
fclose(fidC);
!OpenSees Model.tcl
end
end
end
end
16 个评论
Bob Thompson
2019-2-26
Nested loops work in such a way that for each of the outer loop runs, the entirety of the inner loop is performed. Suppose we have two loops, with indices x and y.
for x = 1:4;
for y = 1:2;
disp([x,y])
end
end
If you run that code you will notice the output begins with both x and y as 1, but on the next output of the disp x remains as 1 while y becomes 2. The inner loop is now complete, and the exterior loop can now proceed. So x becomes 2, and y resets to 1. The fourth row shows that both x and y are two. This continues until all iterations of the outer loop have been completed.
Alternatively, if you have for loops in a series, the first loop runs fully to completion, and then the second runs fully to completion.
x = 1; y = 1;
for x = 1:4;
disp([x,y])
end
for y = 1:2;
disp([x,y])
end
This produces a series of display outputs where x and y both start at 1 again, but on the second disp x will be 2, and y still 1. On the third x becomes 3, with y unchanged, and so on until the end of the first loop. The second loop then executes, and the first pass displays x as 4 and y as 1 a second time, before the final pass of the loop displays x as 4 and y as 2.
Does that help you understand better the difference between series and nested loops?
I would suggest that you need to use nested loops because you want to run your second loop each time you run your first loop.
Also, I would suggest moving count to before the beginning of the first loop (before for i = ...).
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!