Save total sums of a vector in each for loop
4 views (last 30 days)
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
0 Comments
Answers (1)
Walter Roberson
on 7 Mar 2021
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)
0 Comments
See Also
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!