Save total sums of a vector in each for loop

4 views (last 30 days)
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)

Walter Roberson
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)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!