Save total sums of a vector in each for loop
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
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)
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
