What's the difference between
a = [ 1 2 3 ];
and
a = zeros(1,3);
a = [ 1 2 3 ];
Performance? Logic? I'm just interested to know. Thanks for your help!

 Akzeptierte Antwort

Jan
Jan am 20 Apr. 2017

0 Stimmen

In
a = zeros(1,3);
a = [ 1 2 3 ];
Matlab reserves memory for 3 doubles and fills then with zeros in the first line. In the second line this memory is freed and a new vector is created. This needs about the double work compared to creating [1, 2, 3] directly and in consequence is a waste of time only.
This is not a pre-allocation. A pre-allocation would mean, that the reserved memory is re-used later:
a = zeros(1, 3);
a(1) = 1;
a(2) = 2;
a(3) = 3;
or
a = zeros(1, 3);
a(:) = 1:3;
The "(:)" is essential here, because it tells Matlab to overwrite the contents of the formerly existing memory.

6 Kommentare

Ahmed Hossam
Ahmed Hossam am 20 Apr. 2017
Bearbeitet: Ahmed Hossam am 20 Apr. 2017
So, in matlab I don't need to wory about pre-allocation and just write my one line of code?
Ahmed Hossam
Ahmed Hossam am 20 Apr. 2017
Bearbeitet: Ahmed Hossam am 20 Apr. 2017
Use the appropriate preallocation function for the kind of array you want to initialize:
zeros for numeric arrays
cell for character arrays
https://ch.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html
Stephen23
Stephen23 am 20 Apr. 2017
Bearbeitet: Stephen23 am 20 Apr. 2017
@Ahmed Hossam: You are confusing different topics together.
You should preallocate arrays whenever the array would otherwise be repeatedly expanded (for example before any loop): this significantly improves performance (but it is not a fatal error without it). This is what that help page is explaining, which its very first line clearly states: "for and while loops that incrementally increase the size of a data structure each time through the loop can adversely affect performance and memory use"
However this has nothing to do with your question (which does not mention loops, and does not incrementally expand any array). And as such Jan Simon correctly answered your question (which is not about array preallocation required before loops).
a = [];
for k = 1:1e6
a(k) = k;
end
This is allowed in Matlab. Even if a is removed by clear a before the loop, Matlab runs fine. But the iterative growing of the arrays is very expensive: In the first iteration, Matlab reserves memory for 1 double (8 bytes) and assigns the value to the first element. In the next iteration, memory for 1 doubles is reserved, the first element is copied, the former array is released and the new value is copied. Finally, Matlab has reverved and copies not 1e6*8 bytes (8 MB), but sum(1:1e6)*8 bytes: 4 TB! This takes time. With a proper pre-allocation only 8MB are used:
a = zeros(1, 1e6);
for k = 1:1e6
a(k) = k;
end
Ahmed Hossam
Ahmed Hossam am 20 Apr. 2017
Thank you for your help
Ahmed Hossam
Ahmed Hossam am 20 Apr. 2017
Bearbeitet: Ahmed Hossam am 20 Apr. 2017
@ Stephen Cobeldick:
Ok, I understand both topics now.
First topic:
Just deklare and initialize a variable in one line! (beautiful feature!!)
Second topic:
Don't extend the memory of a variable in a loop!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 20 Apr. 2017

Bearbeitet:

am 20 Apr. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by