Do I need pre-allocation ?

조회 수: 20 (최근 30일)
Robin L.
Robin L. 2019년 3월 19일
댓글: Robin L. 2019년 3월 20일
Hello everybody !
You helped me a lot these last days, thanks !
I come up today with another question, concerning preallocation.
Let's consider 2 versions of code :
% a and b some matrix 1D
tab = zeros(length(a), length(b)); % PREALLOCATION
for x_idx = 1:length(a)
for y_idx = 1:length(b)
tab(x_idx, y_idx) = a(x_idx) * b(y_idx);
end
end
And :
% x and y some matrix 1D
tab = zeros(length(x), length(y)); % PREALLOCATION
tab = a' .* b;
I am wondering if I really need preallocation in the second version, because I don't access to every key of an "growing up" array, but I use Matlab vectorization..
So I think I can write (without loosing in terms of perf, and event do a bargain) :
% x and y some matrix 1D
tab = a' .* b;
Am I right ?
Robin.
  댓글 수: 6
Steven Lord
Steven Lord 2019년 3월 19일
In general the zeros function is one tool you can use to preallocate.
In your specific example your call to the zeros function is not preallocation.
Preallocating an array means allocating/initializing an array so you can fill it in later on, as opposed to reallocating the array to be a new size every time you add an element to it.
You're allocating an array, then throwing that allocated array away and reusing that name for a whole new array that has no relation with the originally allocated array.
Robin L.
Robin L. 2019년 3월 20일
I thank you Stephen Cobeldick and Steven Lord ! Now I understand what means pre-allocation ?

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

채택된 답변

Guillaume
Guillaume 2019년 3월 19일
편집: Guillaume 2019년 3월 19일
Robin, in the 2nd case, yes you were attempting to preallocate the array. However, the array you preallocated is not the same array that you will end up with. In fact, the array you preallocated is immediately destroyed and replaced by a completely different array on the next line. Hence why Stephen says it's not preallocation.
In fact, more than unnecessary, your preallocation attempt is counter productive. Matlab waste time preallocating an array that is never going to be used and will be destroyed immediately.
There is a big difference between
tab(r, c) = something %indexing
and
tab = something %no indexing
In the first case, using indexing, you're assigning to one or more elements of the array. If the array is not big enough to start with, then matlab waste time making room for the new element(s). Hence preallocation is important.
In the second case, where there's no indexing, you're copying an entire array. Whatever was in the variable before that gets discarded, so preallocation doesn't work.
  댓글 수: 1
Robin L.
Robin L. 2019년 3월 20일
Thank you Guillaume ! Now I understand what means pre-allocation ?

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

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 3월 19일
Preallocation is not necessary in your second version.
  댓글 수: 3
madhan ravi
madhan ravi 2019년 3월 19일
I said Preallocation is not needed for the second case , where have I ever said it was preallocation?, one flaw was I never explained the reason for it but Guillaume did so +1.
Robin L.
Robin L. 2019년 3월 20일
Thank you madhan ravi ! Now I understand what means pre-allocation ?

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

카테고리

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by