extract matrix from for loop
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
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?
댓글 수: 0
채택된 답변
  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.
댓글 수: 0
추가 답변 (1개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


