zeros - Create array of all zeros - MATLAB (original) (raw)

Create array of all zeros

Syntax

Description

[X](#mw%5F96a7565e-f611-46e5-9c8e-634a6826ea1b) = zeros returns the scalar0.

[X](#mw%5F96a7565e-f611-46e5-9c8e-634a6826ea1b) = zeros([n](#btovuwl-n)) returns ann-by-n matrix of zeros.

example

[X](#mw%5F96a7565e-f611-46e5-9c8e-634a6826ea1b) = zeros([sz1,...,szN](#btovuwl-sz1szN)) returns an sz1-by-...-by-szN array of zeros wheresz1,...,szN indicate the size of each dimension. For example,zeros(2,3) returns a 2-by-3 matrix.

example

[X](#mw%5F96a7565e-f611-46e5-9c8e-634a6826ea1b) = zeros([sz](#btovuwl-sz)) returns an array of zeros where size vector sz defines size(X). For example, zeros([2 3]) returns a 2-by-3 matrix.

example

[X](#mw%5F96a7565e-f611-46e5-9c8e-634a6826ea1b) = zeros(___,[typename](#btovuwl-typename)) returns an array of zeros of data type typename. For example,zeros("int8") returns a scalar, 8-bit integer 0. You can use any of the input arguments in the previous syntaxes.

example

[X](#mw%5F96a7565e-f611-46e5-9c8e-634a6826ea1b) = zeros(___,like=[p](#btovuwl-p)) returns an array of zeros like p; that is, of the same data type (class), sparsity, and complexity (real or complex) as p. You can specify typename or like, but not both.

example

Examples

collapse all

Create a 4-by-4 matrix of zeros.

X = 4×4

 0     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)

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)

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

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

X = 1×3 uint32 row vector

0 0 0

Create a scalar 0 that is complex like an existing array instead of real valued.

First, create a complex vector.

Create a scalar 0 that is complex like p.

Create a 10-by-10 sparse matrix.

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

X = 2×3 sparse double matrix All zero

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 as p.

X = zeros(size(p),like=p)

X = 2×3 uint8 matrix

0 0 0 0 0 0

If you have Parallel Computing Toolbox™, create a 1000-by-1000 distributed array of zeros with underlying data typeint8. For the distributed data type, thelike 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 as p.

X = zeros(size(p),like=p); class(X)

Input Arguments

collapse all

Size of square matrix, specified as an integer value.

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

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

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:

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 provideszeros support.

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

Output Arguments

collapse all

Array of zeros, returned as a scalar, vector, matrix, or multidimensional array.

Extended Capabilities

expand all

Usage notes and limitations:

Dimensions must be nonnegative real integers. Empty dimensions are not supported.

Usage notes and limitations:

Refer to the usage notes and limitations in the C/C++ Code Generation section. The same limitations apply to GPU code generation.

Dimensions must be nonnegative real integers.

The zeros function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Usage notes and limitations:

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced before R2006a