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
2017 年 4 月 20 日
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
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
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
@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
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
2017 年 4 月 20 日
Thank you for your help
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 Exchange で Loops and Conditional Statements についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
