# why do we initialize any vector or matrices with zero.

31 ビュー (過去 30 日間)
Akhil 2023 年 7 月 8 日
コメント済み: Bruno Luong 2023 年 7 月 9 日
before doing any operation with matrices which we want as output, we define a matrix and initialize with zero. why do we do it? what if we don't initialize it? and what if we initialize with other no.
##### 2 件のコメントなしを表示なしを非表示
Sandeep Mishra 2023 年 7 月 8 日
Can you give an example to clarify your question?
Akhil 2023 年 7 月 8 日

Ok, let say we have an input matrix A,which is entered by the user.we want an additional matrix B as output.which will have some elements by modification in elements of A. So, before entering the loop, we consider B= zeros(row,column), why? Why not ones or any other values?

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

### 回答 (3 件)

Voss 2023 年 7 月 8 日
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Walter Roberson 2023 年 7 月 8 日
Internally, initializing with zero is faster than initializing with anything else. But you can initialize with ones() or false() or true() or NaN() or inf() or whatever makes sense in context

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

John D'Errico 2023 年 7 月 8 日

You can preallocate with ANY number. Feel free to preallocate with pi, using say, repmat. Personally, I like to preallocate with NaNs, or maybe the number 17. This way, when I test the code out, I can tell if I screwed up with the size. (What, me screw up? Surely not.)
But you preallocate whenever the array will grow in size otherwise. Read the link proided by @Voss.
Other arrays need not be preallocated.
##### 0 件のコメント-2 件の古いコメントを表示-2 件の古いコメントを非表示

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

James Tursa 2023 年 7 月 9 日

If you know you will be filling in the variable elements downstream anyway, it would be faster to just allocate the memory and not spend the time filling the values with anything. That being said, MATLAB must have some type of optimizations going on in the background for the zeros( ) function because the timings rival a mex routine that allocates but does not initialize:
R2021a PCWIN:
>> N = 1024*1024*1024/8 % 1 GB variable
N =
134217728
>> timeit(@()nan(N,1))
ans =
0.4622
>> timeit(@()inf(N,1))
ans =
0.4729
>> timeit(@()ones(N,1))
ans =
0.4645
>> timeit(@()zeros(N,1))
ans =
0.0134
>> timeit(@()uninit(N,1)) % mex routine that allocates but does not initialize
ans =
0.0135
I would point out that this was not the case in earlier versions of MATLAB, where uninit( ) used to handily beat the zeros( ) function.
##### 3 件のコメント1 件の古いコメントを表示1 件の古いコメントを非表示
Walter Roberson 2023 年 7 月 9 日
In some recent releases (I do not know about the latest release), initializing an array with zeros() was special. If you use zeros() to preallocate an array large enough that you are running close to your memory limits, then zeros() can succeed and appear to give you memory, only to be told that you have run out of memory as soon as you assign into the array. But you can keep the zeros() array around as long as you leave it as pure zeros.
I cannot seem to replicate this at the moment, so it might have changed.
Bruno Luong 2023 年 7 月 9 日
Yes I confirm have seen what James and Walter observes.
The Execution Engine can delay the effectve (zeros) allocation later, when the array is effectively used.
Measuring the allocation time alone doesn't not reflect the real speed.

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

### カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by