How to save all values generated by a for loop into an array?
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2019-12-28
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!