Saving data from loop
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
hi hey
am 21 Jan. 2018
Kommentiert: hi hey
am 21 Jan. 2018
while(run<time)
if(run > time)
break
end
~do calculations~
z=[];
pie= crust*apple;
z=[pie];
~do calculations~
end
I'm trying to store all of the calculations from pie but for some reason it is being overwritten each time it goes through the loop. what else would I need to add to have the data saved correctly?
0 Kommentare
Akzeptierte Antwort
Stephen23
am 21 Jan. 2018
Bearbeitet: Stephen23
am 21 Jan. 2018
method one: concatentation:
z = [];
while ...
...
pie = ...
z = [z,pie];
end
method two: indexing:
replace z = [z,pie] with:
z(end+1) = pie;
Note that expanding z on each iteration will be very inefficient, and will likely slow down your code: it is rarely a good idea to start with an empty array and enlarge it on each loop iteration. See:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements 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!