Storing the results from nested loop
Ältere Kommentare anzeigen
Hello, I have reviewed other posts and videos on this but those solutions did not work - well, not elegantly. How do I store each iteration of the following nested loop?
for a=0:3
A= 2*a
for b=a+1:6
B=3*b
for c=b+1:10
C=4*c
vec=[A B C] % to store output of each iteration
end
plot(vec,'r--') % plot each iteration in its entirety, each time
end
end
Akzeptierte Antwort
Weitere Antworten (1)
You could of course not bother with the loops at all:
a = 0:3;
b = a(1)+1:6; %too many elements, the extra will be removed later
c = b(1)+1:10; %too many elements, the extra will be removed later
[A, B, C] = ndgrid(a, b, c);
abc = [A(:), B(:), C(:)];
%now remove unwanted entries
abc(abc(:, 2) <= abc(:, 1) | abc(:, 3) <= abc(:, 2), :) = [];
%calculate vec and plot all
vec = abc .* [2 3 4];
plot([1 2 3], vec, 'r--');
2 Kommentare
Pit Probst
am 11 Aug. 2018
Guillaume
am 12 Aug. 2018
The orders of the rows is different, but I'm assuming it does not matter. If it does, at the end:
vec = sortrows(vec);
Kategorien
Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!