How can I reduce the runtime of a loop with 6000 iterations?
Show older comments
I want to be able to use a loop for more complex problems with 6000 iterations. This is just an example. I know I can simply use {A = n * 2} to get the answer. I just need to know how to reduce its runtime.
Many thanks!
Z = [];
for n = 1:6000
A = n * 2;
Z = [Z A]
n = n + 1;
end
1 Comment
Stephen23
on 28 Jul 2016
Question: "How can I reduce the runtime of a loop"
Answer: learn to read the MATLAB documentation:
Accepted Answer
More Answers (1)
Star Strider
on 21 Jul 2016
Preallocating would be my first approach:
N = 6000;
Z = zeros(1, N); % Preallocate Z’
for n = 1:N
A = n * 2;
Z(n) = A;
end
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!