MATLAB: Is there a faster way to concantenate this matrix

concatenatematrix

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?

Best Answer

Pre-allocate
A=zeros(2,1000)
A(:,1:2)=A1;
A(:,3:4)=A2;
%and so on