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

19 visualizaciones (últimos 30 días)
Phillip Smith
Phillip Smith el 6 de Feb. de 2020
Editada: Phillip Smith el 6 de Feb. de 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

Respuesta aceptada

Bob Thompson
Bob Thompson el 6 de Feb. de 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 comentario
Phillip Smith
Phillip Smith el 6 de Feb. de 2020
Editada: Phillip Smith el 6 de Feb. de 2020
Thank you for your responce! Exactly what I wanted!
Thank you so much
:)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by