Save total sums of a vector in each for loop
Show older comments
I'm writing a code that includes a for loop and a function that adds all elements in a vector. I need to save each sum of each iteration in a vector.
N=100;
for k=1:N
A = zeros(1,10);
while ~all(A)
e = numel(A);
r = randi(e,1);
A(r) = randi(10);
if all(A)
sum(A)
end
end
end
Answers (1)
N=100;
for k=1:N
A = zeros(1,10);
while ~all(A)
e = numel(A);
r = randi(e,1);
A(r) = randi(10);
end
Asum(k) = sum(A);
end
histogram(Asum)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!