フィルターのクリア

Preallocating the size of a matrix which concatenates horizontally matrices in a for loop

1 回表示 (過去 30 日間)
How can I preallocate matrix t:
t=[];
for i=1:3;
a=randi(10,3,2);
t=[a t];
end

採用された回答

the cyclist
the cyclist 2013 年 6 月 16 日
Like this. (I also parameterized so it would be easier to see how this generalizes.)
m=3;
n=2;
t = zeros(m,m*n);
for i=1:m;
a = randi(10,m,n);
t(:,n*(i-1)+1:n*i) = a;
end
You don't need the intermediate variable, either:
m=3;
n=2;
t = zeros(m,m*n);
for i=1:m;
t(:,n*(i-1)+1:n*i) = randi(10,m,n);
end

その他の回答 (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