Matlab

How to Create a Vector of Zeros in MATLAB

MATLAB stands for matrix laboratory and it provides us with various ways to perform array operations and to create vectors, matrices, and arrays. It enables us to create a vector of zeros in MATLAB using the built-in zeros() function. This function can create a row vector or a column vector by setting up the number of rows or the number of columns equal to 1.

This article will walk through the ways of creating a row or column vector of zeros in MATLAB.

What is a Vector of Zeros? Why is it Useful to Create a Vector of Zeros in MATLAB?

The vector of zeros is a vector consisting of all zero values. Creating a vector of zeros is important in MATLAB because it can be used for varieties of ways, such as:

  • To initialize variables to all zeros
  • To represent the set of unknown values
  • To represent a matrix of zeros

How to Produce a Zeros’ Vector in MATLAB?

The zeros() function is MATLAB’s built-in function that can be utilized to generate a zeros vector. The function takes two arguments as input: the rows’ number and the columns’ number. If the number of rows is set to 1, the function will create a row vector of zeros. If the number of columns is set to 1, the function will create a column vector of zeros.

Syntax

The syntax of the zeros() function for creating a row or column vector of zeros in MATLAB is given below:

zeros(1,n)
zeros(n,1)

Here,

The function zeros(1,n) create a row vector of zeros, while zeros(n,1) create a column vector having all zero elements.

Return Value

This function takes an array’s size as input and returns an array having all zero entries.

Example 1: How to Generate a Row Vector of Zeros in MATLAB?

This example shows how to create a row vector of zeros in MATLAB using the zeros(1, n) function.

row_vect = zeros(1, 9)

Example 2: How to Generate a Zeros Column Vector in MATLAB?

In this example, we create a column vector of zeros having a length 9-by-1 using the zeros(n,1) function in MATLAB.

col_vect = zeros(9, 1)

Example 3: How to Create a Vector of Zeros in MATLAB By Specifying the Vector Type?

We can also create a vector of zeros in MATLAB by specifying the vector type that could be double, single, int8, uint8, int16, uint16, and more. The following example will create a column vector of zeros of type int16.

vect_zeros= zeros(9, 1, 'int16')

 

Conclusion

MATLAB is a robust programming language that allows us to create vectors, matrices, and arrays using various built-in functions. One such function is the zeros() function which allows us to create a vector of zeros of the specified size. This guide has provided an easy guide to creating a vector of zeros using MATLAB’s zeros() function. The examples provided in this guide will help you create a row or column vector of zeros in MATLAB.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.