zeros
Creates an array of zeros.
Category: Language Fundamentals > Matrices and Arrays
Syntax
Z = zeros(n)
Returns an n-by-n matrix of zeros.
Z = zeros(m, n, ...)
Returns an array of zeros with the given dimensions.
Z = zeros(sz)
Takes the size from the vector sz.
Z = zeros(___, classname)
Creates zeros of the given class, such as 'int8' or 'single'.
Z = zeros(___, 'like', P)
Matches the class (and complexness) of the prototype P.
Description
zeros is mainly used to preallocate: creating the output array up front and filling it inside a loop is much faster than growing it one element at a time.
A dimension of 0 gives an empty array, for example zeros(0,3). Calling zeros with no arguments returns the scalar 0.
Input arguments
n, m, ...- Nonnegative integer dimensions. Negative values count as 0.
classname- Output class; the default is
'double'.
Output arguments
Z- Array of zeros of the requested size and class.
Example
zeros(2, 3)
zeros(2, 'int8')
size(zeros(0, 3))
Output
ans = 2×3
0 0 0
0 0 0
ans = 2×2 int8 matrix
0 0
0 0
ans = 1×2
0 3