How to save all values generated by a for loop into an array?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sarfraz Khan
am 28 Dez. 2019
Beantwortet: Star Strider
am 28 Dez. 2019
Please help! I have been trying to save the numerical value of each 'counter' function but as it is in a for loop the value for counter is overwritten each time? I have copied the code below:
for n=1:100
die1= randi(6);
die2 = randi(6);
dicesum = die1+die2;
counter=0;
while dicesum<12
counter = counter+1;
die1 = randi(6);
die2 = randi(6);
dicesum = die1+die2;
end
counter
end
0 Kommentare
Akzeptierte Antwort
Star Strider
am 28 Dez. 2019
Subscript it:
counterv = nan(1,100);
for n=1:100
die1= randi(6);
die2 = randi(6);
dicesum = die1+die2;
counter=0;
while dicesum<12
counter = counter+1;
die1 = randi(6);
die2 = randi(6);
dicesum = die1+die2;
end
counterv(n) = counter
end
I am not excatly certain what you want to do or how your code is organised. This saves the value of ‘counter’ in every iteration as an element of ‘counterv’ (counter vector).
Make appropriate changes so the code does what you want it to do.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!