필터 지우기
필터 지우기

Faster way to concatenate arrays with unknown size ?

조회 수: 9 (최근 30일)
RuiQi
RuiQi 2016년 7월 7일
편집: per isakson 2016년 7월 7일
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..

채택된 답변

Walter Roberson
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
per isakson 2016년 7월 7일
편집: per isakson 2016년 7월 7일
"know the maximum size it can take" &nbsp
One approach: Assume the result_array is &nbsp<kxm double>. Preallocate total_result as &nbsp<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
Walter Roberson
Walter Roberson 2016년 7월 7일
Yes, removing extra rows afterwards is efficient.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by