Decomposing Matrix into Column Matrices

I have a large nxm matrix, lets call it A, and I want to break it up so that each column of A is now its own nx1 column matrix. So for example if I have:
A=[1 2 3; 4 5 6; 7 8 9]
What I want is:
A1=[1 ; 4 ; 7] A2=[2 ; 5 ; 8] A3=[3 ; 6 ; 9]
Is there some function or program that will do this for me?
Thanks

 채택된 답변

the cyclist
the cyclist 2011년 7월 14일

0 개 추천

I recommend using cell arrays, rather than naming your variables A1, A2, etc:
numberColumns = size(A,2);
a = cell(1, numberColumns);
for nc = 1:numberColumns
a{nc} = A(:,nc); % Note the curly brackets for a{nc}
end

댓글 수: 5

Oleg Komarov
Oleg Komarov 2011년 7월 14일
See also faq 4.6.
Andrei Bobrov
Andrei Bobrov 2011년 7월 14일
a = arrayfun(@(i1)A(:,i1),1:size(A,2),'un',0)
Jan
Jan 2011년 7월 14일
@Cyclist: It is worth to include the pre-allocation "a = cell(1, numberColumns)".
the cyclist
the cyclist 2011년 7월 14일
@Jan: How could I neglect that?! I've edited the code to include preallocation.
David
David 2011년 7월 15일
Thanks a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Software Development Tools에 대해 자세히 알아보기

질문:

2011년 7월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by