Basic question about initializing variables

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!

 採用された回答

Jan
Jan 2017 年 4 月 20 日

0 投票

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 件のコメント

Ahmed Hossam
Ahmed Hossam 2017 年 4 月 20 日
編集済み: Ahmed Hossam 2017 年 4 月 20 日
So, in matlab I don't need to wory about pre-allocation and just write my one line of code?
Ahmed Hossam
Ahmed Hossam 2017 年 4 月 20 日
編集済み: Ahmed Hossam 2017 年 4 月 20 日
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 2017 年 4 月 20 日
編集済み: Stephen23 2017 年 4 月 20 日
@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).
Jan
Jan 2017 年 4 月 20 日
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 2017 年 4 月 20 日
Thank you for your help
Ahmed Hossam
Ahmed Hossam 2017 年 4 月 20 日
編集済み: Ahmed Hossam 2017 年 4 月 20 日
@ 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!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2017 年 4 月 20 日

編集済み:

2017 年 4 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by