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

18 ビュー (過去 30 日間)
Phillip Smith
Phillip Smith 2020 年 2 月 6 日
編集済み: Phillip Smith 2020 年 2 月 6 日
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.
  1 件のコメント
Phillip Smith
Phillip Smith 2020 年 2 月 6 日
編集済み: Phillip Smith 2020 年 2 月 6 日
Thank you for your responce! Exactly what I wanted!
Thank you so much
:)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by