Creating variables (matrices) in for loop
2 ビュー (過去 30 日間)
古いコメントを表示
Hello, I'd like to create new variables in a for loop that are matrices. It wolud be something along the lines of:
for i=1:n
ki=
[a(i) b(i) a(i)*b(i) -a(i)*b(i);.......;etc.]
So it would create variables k1,k2...kn with the first row if k1 being a(1) b(1) etc. Any way to do this? This would really help simplify my code.
0 件のコメント
採用された回答
Marc Jakobi
2016 年 10 月 10 日
編集済み: Marc Jakobi
2016 年 10 月 10 日
Creating new variables dynamically is bad practice. Store them in a cell array or in a struct. For example:
C = cell(n,1);
for i = 1:n
A{i} = [a(i) b(i) a(i)*b(i) -a(i)*b(i);.......;etc.];
end
This article was written for Python, but it also applies to Matlab. Here's another article on the topic.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!