MATLAB: Faster way to concatenate arrays with unknown size

arrayconcatenate

In my application, I have a huge for loop and at each iteration, I concatenate the 'result' array with the matrix obtained at the current loop. The function returns this 'result' array at the end of the for loop. Is there a way I can speed things up because concatenating seems very slow when the result array gets large enough. I won't know the final size of the array but I will know the size of the matrix at the current loop if that helps..

Best Answer

If you know the final data size then pre-allocate the output and store into the array as you go.
If you do not know the final data size then store the pieces in a cell array until the end (better yet if you can at least pre-allocate the cell array itself.) You can then cell2mat()