How to save all values generated by a for loop into an array?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sarfraz Khan
el 28 de Dic. de 2019
Respondida: Star Strider
el 28 de Dic. de 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 comentarios
Respuesta aceptada
Star Strider
el 28 de Dic. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!