Adding to a vector after each step of a for loop.

12 views (last 30 days)
Hey everyone,
So I have a bit of code, its function is irrelevant, that displays the number of timsteps that the it (the code) ran for i.e I run the code and in the console I'd get a number lke 354.
I put this whole code into a for loop so that the code runs 1000 times and thus in the console 1000 different values for the run time are displayed.
What I want to do is store all these values for the run time into a column vector. I've tried initilising a vector of zeros and then replacing the 0 with the runtime but i can't figure out how to get the 1st runtime into the first element of the vector and so on and so forth.
If anyone could lend a hand it would be greatly appreciated!
phill

Accepted Answer

Bob Thompson
Bob Thompson on 6 Feb 2020
You need to specify the index of the element you want to replace.
for i = 1:1000;
steps(i) = i;
end
Realistically though, if all you're doing is counting the number of steps then you can just create an array of integers.
steps = [1:i]'; % Put this after the loop for max number of steps completed.
  1 Comment
Phillip Smith
Phillip Smith on 6 Feb 2020
Edited: Phillip Smith on 6 Feb 2020
Thank you for your responce! Exactly what I wanted!
Thank you so much
:)

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!