zeros - Create array of all zeros - MATLAB (original) (raw)
Create array of all zeros
Syntax
Description
X = zeros
returns the scalar 0
.
X = zeros([n](#btovuwl-n))
returns an n
-by-n
matrix of zeros.
X = zeros([sz1,...,szN](#btovuwl-sz1szN))
returns an sz1
-by-...-by-szN
array of zeros where sz1,...,szN
indicate the size of each dimension. For example, zeros(2,3)
returns a 2-by-3 matrix.
X = 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.
X = 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.
X = 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.
Examples
Matrix of Zeros
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
3-D Array of Zeros
Create a 2-by-3-by-4 array of zeros.
X = zeros(2,3,4); size(X)
Clone Size from Existing Array
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:
Specify Data Type of Zeros
Create a 1-by-3 vector of zeros whose elements are 32-bit unsigned integers.
X = 1x3 uint32 row vector
0 0 0
Clone Complexity from Existing Array
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
.
Clone Sparsity from Existing Array
Create a 10-by-10 sparse matrix.
Create a 2-by-3 matrix of zeros that is sparse like p
.
X = 2x3 sparse double matrix All zero
Clone Size and Data Type from Existing Array
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 = 2x3 uint8 matrix
0 0 0 0 0 0
Clone Distributed Array
If you have Parallel Computing Toolbox™, create a 1000-by-1000 distributed array of zeros with underlying data typeint8
. For the distributed
data 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 as p
.
X = zeros(size(p),'like',p);
Input Arguments
n
— Size of square matrix
integer value
Size of square matrix, specified as an integer value.
- If
n
is0
, thenX
is an empty matrix. - If
n
is negative, then it is treated as0
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
sz1,...,szN
— Size of each dimension (as separate arguments)
integer values
Size of each dimension, specified as separate arguments of integer values.
- If the size of any dimension is
0
, thenX
is an empty array. - If the size of any dimension is negative, then it is treated as
0
. - Beyond the second dimension,
zeros
ignores 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
sz
— Size of each dimension (as a row vector)
integer values
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 is
0
, thenX
is an empty array. - If the size of any dimension is negative, then it is treated as
0
. - Beyond the second dimension,
zeros
ignores 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
typename
— Data type (class) to create
'double'
(default) | 'single'
| 'logical'
| 'int8'
| 'uint8'
| ...
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 provides zeros
support.
p
— Prototype of array to create
array
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
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Dimensions must be nonnegative real integers. Empty dimensions are not supported.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
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.
HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.
Dimensions must be nonnegative real integers.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The zeros
function supports GPU array input with these usage notes and limitations:
- You can specify
typename
as'gpuArray'
. If you specifytypename
as'gpuArray'
, the default underlying type of the array isdouble
.
To create a GPU array with underlying typedatatype
, specify the underlying type as an additional argument beforetypename
. For example,X = zeros(3,datatype,'gpuArray')
creates a 3-by-3 GPU array of zeros with underlying typedatatype
.
You can specify the underlying typedatatype
as one of these options:'double'
'single'
'logical'
'int8'
'uint8'
'int16'
'uint16'
'int32'
'uint32'
'int64'
'uint64'
- You can also specify the numeric variable
p
as agpuArray
.
If you specifyp
as agpuArray
, the underlying type of the returned array is the same asp
.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
- You can specify
typename
as'codistributed'
or'distributed'
. If you specifytypename
as'codistributed'
or'distributed'
, the default underlying type of the returned array isdouble
.
To create a distributed or codistributed array with underlying typedatatype
, specify the underlying type as an additional argument beforetypename
. For example,X = zeros(3,datatype,'distributed')
creates a 3-by-3 distributed matrix of zeros with underlying typedatatype
.
You can specify the underlying typedatatype
as one of these options:'double'
'single'
'logical'
'int8'
'uint8'
'int16'
'uint16'
'int32'
'uint32'
'int64'
'uint64'
- You can also specify
p
as acodistributed
ordistributed
array.
If you specifyp
as acodistributed
ordistributed
array, the underlying type of the returned array is the same asp
. - For additional
codistributed
syntaxes, see zeros (codistributed) (Parallel Computing Toolbox).
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a