Matlab

How to Create an Array of Zeros in MATLAB

MATLAB is a high-level programming environment used for creating algorithms and testing mathematical data. Using MATLAB, we can create and analyze data in the form of graphs. To plot graphs arrays can be used and we may need to plot an array of the same value as zeros or ones.

This article covers ways of plotting an array of zeros in MATLAB.

Creating an Array of Zeros in MATLAB

The zeros() function can create an array of zeros in MATLAB. This function takes one or more arguments that specify the size of the array we want to create.

Syntax of the zeros Function

The basic syntax for creating an array of zeros using the zeros() function is as follows:

A = zeros(n)

where n is a positive integer that defines the total array elements.

Examples of Using the zeros Function

Here are some examples of using the zeros function to create arrays of different sizes:

% Create a row vector of 5 zeros

A = zeros(1,5)

% Create a column vector of 5 zeros

B = zeros(5,1)

% Create a 3x3 matrix of zeros

C = zeros(3)

Creating a Multidimensional Array of Zeros

In addition to creating vectors and matrices, we can also use the zeros function to create multidimensional arrays (i.e., arrays with more than two dimensions) of zeros. To do this, we need to specify the size of each dimension as separate arguments.

For example, to create a 3x4x2 array (i.e., with 3 rows, 4 columns, and 2 pages) of zeros, run below MATLAB code:

A = zeros(3,4,2)

Specifying the Data Type of the Array

By default, the zeros() function creates an array with elements of type double. However, we can also specify a different data type for the elements by providing an additional argument.

To create an array of zeros with elements of type int8, use the following MATLAB code:

A = zeros(3,'int8')

To create an array of zeros with elements of type int32, use the following MATLAB code:

X = zeros(2, 3, 'int32')

Creating a Scalar Zero

The zeros() function can also be used to create a scalar zero. To do this, simply remove the rows and column arguments of this function. For example, the following MATLAB code will create a scalar zero:

X = zeros()

Conclusion

This article covers ways of creating an array of zeros in MATLAB. To create an array of zeros the zeros() function is used. Using this function, we can also create multidimensional arrays of zero and we can also define a scalar zero by removing the arguments of this function. Read more about the ways of defining zeros() in this article.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.