Main Content

zeros

Create array of all zeros

Description

X = zerosreturns the scalar0.

example

X = zeros(n)returns ann-by-nmatrix of zeros.

example

X = zeros(sz1,...,szN)returns ansz1-by-...-by-szNarray of zeros wheresz1,...,szNindicate the size of each dimension. For example,zeros(2,3)returns a 2-by-3 matrix.

example

X = zeros(sz)returns an array of zeros where size vectorszdefinessize(X). For example,zeros([2 3])returns a 2-by-3 matrix.

example

X = zeros(___,typename)returns an array of zeros of data typetypename. For example,zeros('int8')returns a scalar, 8-bit integer0. You can use any of the input arguments in the previous syntaxes.

example

X = zeros(___,'like',p)returns an array of zeros likep; that is, of the same data type (class), sparsity, and complexity (real or complex) asp. You can specifytypenameor'like', but not both.

Examples

collapse all

Create a 4-by-4 matrix of zeros.

X = zeros(4)
X =4×40 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Create a 2-by-3-by-4 array of zeros.

X = zeros(2,3,4); size(X)
ans =1×32 3 4

Create an array of zeros that is the same size as an existing array.

A = [1 4; 2 5; 3 6]; sz = size(A); X = zeros(sz)
X =3×20 0 0 0 0 0

It is a common pattern to combine the previous two lines of code into a single line:

X = zeros(size(A));

Create a 1-by-3 vector of zeros whose elements are 32-bit unsigned integers.

X = zeros(1,3,'uint32')
X =1x3 uint32 row vector0 0 0
class(X)
ans = 'uint32'

Create a scalar0that is complex like an existing array instead of real valued.

First, create a complex vector.

p = [1+2i 3i];

Create a scalar0that is complex likep.

X = zeros('like',p)
X = 0.0000 + 0.0000i

Create a 10-by-10 sparse matrix.

p = sparse(10,10,pi);

Create a 2-by-3 matrix of zeros that is sparse likep.

X = zeros(2,3,'like',p)
X = All zero sparse: 2x3

Create a 2-by-3 array of 8-bit unsigned integers.

p = uint8([1 3 5; 2 4 6]);

Create an array of zeros that is the same size and data type asp.

X = zeros(size(p),'like',p)
X =2x3 uint8 matrix0 0 0 0 0 0
class(X)
ans = 'uint8'

If you have Parallel Computing Toolbox™, create a 1000-by-1000 distributed array of zeros with underlying data typeint8. For thedistributeddata type, the'like'syntax clones the underlying data type in addition to the primary data type.

p = zeros(1000,'int8','distributed');
Starting parallel pool (parpool) using the 'local' profile ... connected to 6 workers.

Create an array of zeros that is the same size, primary data type, and underlying data type asp.

X = zeros(size(p),'like',p);
class(X)
ans = 'distributed'
underlyingType(X)
ans = 'int8'

Input Arguments

collapse all

Size of square matrix, specified as an integer value.

  • Ifnis0, thenXis an empty matrix.

  • Ifnis negative, then it is treated as0.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Size of each dimension, specified as separate arguments of integer values.

  • If the size of any dimension is0, thenXis an empty array.

  • If the size of any dimension is negative, then it is treated as0.

  • Beyond the second dimension,zerosignores trailing dimensions with a size of1. For example,zeros(3,1,1,1)produces a 3-by-1 vector of zeros.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Size of each dimension, specified as a row vector of integer values. Each element of this vector indicates the size of the corresponding dimension:

  • If the size of any dimension is0, thenXis an empty array.

  • If the size of any dimension is negative, then it is treated as0.

  • Beyond the second dimension,zerosignores trailing dimensions with a size of1. For example,zeros([3 1 1 1])produces a 3-by-1 vector of zeros.

Example:sz = [2 3 4]creates a 2-by-3-by-4 array.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Data type (class) to create, specified as'double','single','logical','int8','uint8','int16','uint16','int32','uint32','int64','uint64', or the name of another class that provideszerossupport.

Prototype of array to create, specified as an array.

Data Types:double|single|logical|int8|int16|int32|int64|uint8|uint16|uint32|uint64
Complex Number Support:Yes

Extended Capabilities

Introduced before R2006a