How to put the second "for" loop inside the first one?

2 views (last 30 days)
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 Comments
Bob Thompson
Bob Thompson on 26 Feb 2019
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 = ...).
Ismail Qeshta
Ismail Qeshta on 27 Feb 2019
Edited: Ismail Qeshta on 27 Feb 2019
Hi Bob,
Many thanks for your explanation.
I have thought of the code and modified it. It finally works.
Yes, you are right. I need nested "for" loop.
I have also shifted the counter above the first loop, but counted only after the second loop.
The code is shown below for your kind reference.
Thank you very much for all your time and effort. That was a great learning exercise.
close all; clc; clear all;
a = [100];
b = [100];
count = 0; % Initialize
for i=1:1;
for j=1:1;
line1 = ['set m1 ' num2str(a(i)) ';'];
line2 = ['set m1 ' num2str(b(j)) ';'];
fid=fopen('MatParam8.0.tcl','w');
fprintf(fid,'%s\n',line1);
fprintf(fid,'%s\n',line2);
fid=fclose('all');
fin = 'result_all.txt';
s = dlmread(fin,'');
c=[0 0];
t=[0 0];
for m = 1:2;
count = count + 1;
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',count);
fclose(fidP);
fclose(fidC);
!OpenSees Model.tcl
end
end
end

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!