How do I create a matrix that expands with new column vectors added to it?

5 ビュー (過去 30 日間)
Dorian Serafin
Dorian Serafin 2020 年 2 月 19 日
編集済み: Bhaskar R 2020 年 2 月 19 日
I have a for loop, at the end of each loop I get a column vector (always the same length). I would like to store that column in a matrix that keeps expanding.
So if my loop executes n times (the amount n isn't predetermined), at the end I would like to have a matrix with n columns. How do I do this?
I've looked up adding vectors to matrices, merging etc but couldn't find what I was looking for.

採用された回答

Bhaskar R
Bhaskar R 2020 年 2 月 19 日
編集済み: Bhaskar R 2020 年 2 月 19 日
% it is recommended to prellocate x before operation but in your case x is not predetermined so assign columns directly
n = 5;
for ii = 1:n
x(:, ii) = rand(10, 1); % for each iteration the column will be added to x for n(here 5) time
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by