Faster way to concatenate arrays with unknown size ?
16 ビュー (過去 30 日間)
古いコメントを表示
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..
0 件のコメント
採用された回答
Walter Roberson
2016 年 7 月 7 日
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()
3 件のコメント
per isakson
2016 年 7 月 7 日
編集済み: per isakson
2016 年 7 月 7 日
"know the maximum size it can take"  
One approach: Assume the result_array is  <kxm double>. Preallocate total_result as  <kxmxn double> where n is maximum number of loops that can occur.
total_result = zeros(k,m,n)
loop over huge number
total_result(:,:,jj) = result_array;
end
delete unused space of total_result
その他の回答 (0 件)
参考
カテゴリ
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!