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!
采纳的回答
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 个评论
So, in matlab I don't need to wory about pre-allocation and just write my one line of code?
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
@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
Thank you for your help
@ 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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
