Is there a faster way to concantenate this matrix?
조회 수: 6 (최근 30일)
이전 댓글 표시
I have a matrix A that is initially a 2 x 2 matrix.
I compute a new value of A and call this A_new which is also a 2 x 2 matrix
I concatenate the matrix by A = [A A_new] and store it back into A.
I compute another value and call it A_new2 and continue this. Essentially, I am doing the following :
A = [A A_new A_new2 A_new3 ....]
I think this is slowing me down since the size of A grows. Is there a better way to do this?
댓글 수: 0
채택된 답변
Azzi Abdelmalek
2013년 3월 8일
Pre-allocate
A=zeros(2,1000)
A(:,1:2)=A1;
A(:,3:4)=A2;
%and so on
댓글 수: 3
Jan
2013년 3월 11일
@Azzi: Did you edit your answer after John's comment? If so, you see how confusing his comment gets by this. Please avoid such confusions by adding new code instead of changing the existing without a notice. If you didn't, I'm confused by John's comment and the accepting.
Azzi Abdelmalek
2013년 3월 11일
Jan, I don't remember that, but if I did it, The forum editor should mark it as edited
추가 답변 (1개)
Matt J
2013년 3월 8일
C{1}=A
C{2}=A_new
etc...
Then, at the end, you would do
A=horzcat(C{:});
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!