Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# zeros

Create array of all zeros

## Syntax

• X = zeros(classname)
• X = zeros(n,classname)
• X = zeros(sz1,...,szN,classname) example
• X = zeros(sz,classname)
• X = zeros('like',p) example
• X = zeros(n,'like',p)
• X = zeros(sz1,...,szN,'like',p) example
• X = zeros(sz,'like',p) example

## Description

X = zeros returns the scalar 0.

example

X = zeros(n) returns an n-by-n matrix of zeros.

example

X = zeros(sz1,...,szN) returns an sz1-by-...-by-szN array of zeros where sz1,...,szN indicates the size of each dimension. For example, zeros(2,3) returns a 2-by-3 array of zeros.

example

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

X = zeros(classname) returns a scalar 0 where the string, classname, specifies the data type. For example, zeros('int8') returns a scalar, 8-bit integer 0.

X = zeros(n,classname) returns an n-by-n array of zeros of data type classname.

example

X = zeros(sz1,...,szN,classname) returns an sz1-by-...-by-szN array of zeros of data type classname.

X = zeros(sz,classname) returns an array of zeros where the size vector, sz, defines size(X) and classname defines class(X).

example

X = zeros('like',p) returns a scalar 0 with the same data type, sparsity, and complexity (real or complex) as the numeric variable, p.

X = zeros(n,'like',p) returns an n-by-n array of zeros like p.

example

X = zeros(sz1,...,szN,'like',p) returns an sz1-by-...-by-szN array of zeros like p.

example

X = zeros(sz,'like',p) returns an array of zeros like p where the size vector, sz, defines size(X).

## Examples

expand all

### Square Array of Zeros

Create a 4-by-4 array of zeros.

`X = zeros(4)`
```X =

0     0     0     0
0     0     0     0
0     0     0     0
0     0     0     0```

### 3-D Array of Zeros

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

```X = zeros(2,3,4);

size(X)```
```ans =

2     3     4```

### Size Defined by Existing Array

Define a 3-by-2 array A.

```A = [1 4 ; 2 5 ; 3 6];

sz = size(A)
```
```sz =

3     2```

Create an array of zeros that is the same size as A

`X = zeros(sz)`
```X =

0     0
0     0
0     0```

### Nondefault Numeric Data Type

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

```X = zeros(1,3,'uint32'),
class(X)```
```X =

0           0           0

ans =

uint32```

### Complex Zero

Create a scalar 0 that is not real valued, but instead is complex like an existing array.

Define a complex vector.

```p = [1+2i 3i];

```

Create a scalar 0 that is complex like p.

```X = zeros('like',p)
```
```X =

0.0000 + 0.0000i```

### Sparse Array

Define a 10-by-10 sparse matrix.

```p = sparse(10,10,pi);
```

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

`X = zeros(2,3,'like',p)`
```X =

All zero sparse: 2-by-3```

### Size and Numeric Data Type Defined by Existing Array

Define 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),
class(X)```
```X =

0    0    0
0    0    0

ans =

uint8```

## Input Arguments

expand all

### n — Size of square matrixinteger value

Size of square matrix, specified as an integer value, defines the output as a square, n-by-n matrix of zeros.

• If n is 0, then X is an empty matrix.

• If n is negative, then it is treated as 0.

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

### sz1,...,szN — Size of each dimensiontwo or more integer values

Size of each dimension, specified as two or more integer values, defines X as a sz1-by...-by-szN array.

• If the size of any dimension is 0, then X is an empty array.

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

• If any trailing dimensions greater than 2 have a size of 1, then the output, X, does not include those dimensions.

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

### sz — Output sizerow vector of integer values

Output size, 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 is 0, then X is an empty array.

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

• If any trailing dimensions greater than 2 have a size of 1, then the output, X, does not include those dimensions.

Example: sz = [2,3,4] defines X as a 2-by-3-by-4 array.

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

### classname — Output class'double' (default) | 'single' | 'int8' | 'uint8' | ...

Output class, specified as 'double', 'single', 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', or 'uint64'.

Data Types: char

### p — Prototypenumeric variable

Prototype, specified as a numeric variable.

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