Is there a faster way to concantenate this matrix?
Show older comments
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?
Accepted Answer
More Answers (1)
Matt J
on 8 Mar 2013
C{1}=A
C{2}=A_new
etc...
Then, at the end, you would do
A=horzcat(C{:});
1 Comment
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.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!