How to Loop variable names

2 ビュー (過去 30 日間)
Karl
Karl 2013 年 6 月 12 日
Does anybody know how to write a loop that does the same as this:
Y1 = zeros(5,8);
Y2 = zeros(5,8);
The loop below does not do the same thing.
X = {'1', '2'};
nX = length(X);
for iX = 1:nX;
Y{iX} = zeros(5,8);
end

回答 (1 件)

Matt J
Matt J 2013 年 6 月 12 日
編集済み: Matt J 2013 年 6 月 12 日
There are ways, but they are discouraged emphatically. Why is Y1, Y2 preferable to Y{1} and Y{2}? The latter are much easier to index and loop over as you've seen.
Also, since Y1 and Y2 are the same size, it's even better to store them as Y(:,:,1) and Y(:,:,2).
  1 件のコメント
Karl
Karl 2013 年 6 月 12 日
I now see that it is easy to work in the format the loop makes. Thanks for making it clear!

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

カテゴリ

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