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

12 次查看(过去 30 天)
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

采纳的回答

Bob Thompson
Bob Thompson 2020-2-6
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by