Is there a faster way to concantenate this matrix?

6 vues (au cours des 30 derniers jours)
John
John le 8 Mar 2013
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?

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 8 Mar 2013
Pre-allocate
A=zeros(2,1000)
A(:,1:2)=A1;
A(:,3:4)=A2;
%and so on
  3 commentaires
Jan
Jan le 11 Mar 2013
@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
Azzi Abdelmalek le 11 Mar 2013
Jan, I don't remember that, but if I did it, The forum editor should mark it as edited

Connectez-vous pour commenter.

Plus de réponses (1)

Matt J
Matt J le 8 Mar 2013
C{1}=A
C{2}=A_new
etc...
Then, at the end, you would do
A=horzcat(C{:});
  1 commentaire
Matt J
Matt J le 8 Mar 2013
Modifié(e) : Matt J le 8 Mar 2013
It is better, in this approach to pre-allocate C
C=cell(1,N);
Even if you do not know precisely what N will be, it is better to pre-allocate with an over-estimated N, than nothing at all.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by