Save total sums of a vector in each for loop

4 Ansichten (letzte 30 Tage)
Eric Junaeus
Eric Junaeus am 7 Mär. 2021
Beantwortet: Walter Roberson am 7 Mär. 2021
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

Antworten (1)

Walter Roberson
Walter Roberson am 7 Mär. 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)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by