recall updated vector from a loop

1 回表示 (過去 30 日間)
Gaetano Pavone
Gaetano Pavone 2019 年 10 月 23 日
コメント済み: Guillaume 2019 年 10 月 23 日
I have a column vector "displacement" with 18 elements, which is updated at every iteration (Nsteps) in a loop.
I would like to obtain a matrix such that each column is the displacement vector at each iteration. How can I do this?

回答 (1 件)

Joe Vinciguerra
Joe Vinciguerra 2019 年 10 月 23 日
nLoops = 5; % number of times to loop
nElem = 18; % number of elements in each vector (let's use it as long as you know what it is
combinedResults = zeros(nElem,nLoops); % it's good practice to preallocate variables generated in a loop
for i = 1:nLoops
displacement = rand(nElem,1); % I don't have your data, so I'm using rand to simulate it
combinedResults(:,i) = displacement; % Here's where it sounds like you need help. The column you save to increments with i.
end
  1 件のコメント
Guillaume
Guillaume 2019 年 10 月 23 日
+1 for the use of good variable names, and for the preallocation.

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

カテゴリ

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

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by