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

18 vues (au cours des 30 derniers jours)
Phillip Smith
Phillip Smith le 6 Fév 2020
Modifié(e) : Phillip Smith le 6 Fév 2020
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

Réponse acceptée

Bob Thompson
Bob Thompson le 6 Fév 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 commentaire
Phillip Smith
Phillip Smith le 6 Fév 2020
Modifié(e) : Phillip Smith le 6 Fév 2020
Thank you for your responce! Exactly what I wanted!
Thank you so much
:)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by