extract matrix from for loop

2 ビュー (過去 30 日間)
darksideofthemoon101
darksideofthemoon101 2011 年 5 月 16 日
Hi,
I want to extract a 2x2 matrix from each iteration of a for loop. Normally I would use something like
for loop=1:1:10
m=[a b c d];
m2(loop)=m;
end
but this doesn't work because of the increased dimensions. Can anyone help?

採用された回答

Matt Fig
Matt Fig 2011 年 5 月 16 日
Use cell arrays, or stack along the third dimension.
for loop=1:1:10
m=[a b c d]; % I assume this is only a place-holder for code.
m2{loop}=m; % Notice the curly braces, not parenthesis.
end
Or,
for loop=1:1:10
m=[a b c d]; % I assume this is only a place-holder for code.
m2(:,:loop)=m; % A 3D array. Each page is a matrix
end
Also note, that if your final array m2 will be much larger than this you will definitely want to pre-allocate the space.

その他の回答 (1 件)

Sean de Wolski
Sean de Wolski 2011 年 5 月 16 日
m2 = repmat(m,[1 10]);
or
m2 = repmat(m,[10 1]);

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by