Matlab

How to Create an Empty Vector in MATLAB

Creating an empty vector in MATLAB is a common task when working with arrays and data manipulation. An empty vector is a vector that has no elements initially but can be populated with values later. This article aims to provide a straightforward guide on how to create an empty vector in MATLAB.

What is an Empty Vector?

An empty vector is a vector having no elements or values initially. It is a vector with a length of zero, meaning it does not contain any data. An empty vector is often used as a starting point for storing data or initializing arrays that will be populated later on. It is widely used by MATLAB users for different objectives like transferring data from one array to another or adding a row or column in a matrix.

What are the Methods of Creating an Empty Vector in MATLAB?

We can create an empty vector in MATLAB:

Let’s discuss these methods in detail.

1: Using the Square Brackets

One straightforward method to create an empty vector in MATLAB is by using empty square brackets ([]). Simply assign an empty set of brackets to a variable, and MATLAB will create an empty vector of the appropriate size.

Example

The given MATLAB code illustrates how to create an empty vector using the empty brackets.

vect = []

2: Using ClassName.empty Method

You can also use the ClassName.empty method to create an empty vector in MATLAB. For this purpose, you just need to replace ClassName with the desired class name for your vector, such as double, int32, or logical.

A syntax followed by ClassName.empty method in MATLAB is given below:

vect = ClassName.empty

vect = ClassName.empty(sz1,...,szN)

vect = ClassName.empty(sizeVector)

Here:

vect = ClassName.empty returns a 0-by-0 vector of the class specified by ClassName. We must substitute the actual class name for “ClassName.”

vect = ClassName.empty(sz1,…,szN) yields an empty array with the given dimensions. Keep in mind that at least one dimension must have a value of zero.

vect = ClassName.empty(sizeVector) yields an empty array with the given dimensions. Keep in mind that at least one dimension must have a value of zero. To create an empty array having the identical dimension as an existing one use the above syntax. Pass the size function’s return values as inputs.

Example 1

This MATLAB code defines an empty array of characters using char.empty method. Here char is the class name.

x = char.empty

Example 2

This MATLAB code defines an empty array of int32-type elements using int32.empty method. Here int32 is the class name.

x = int32.empty

Example 3

The following code creates an empty array of elements having double data type using double.empty method. Here double is the class name.

x = double.empty

Example 4

The following example creates a 1×0 empty array of int16 class in MATLAB.

x = int16.empty(1, 0)

Conclusion


Creating an empty vector in MATLAB is a fundamental task when working with arrays and data manipulation. It provides a starting point for storing data and initializing arrays that will be populated later on. This article presented two methods for creating empty vectors: using empty square brackets [] and using the ClassName.empty method. By following these methods, users can easily create empty vectors of different classes and dimensions to suit their specific needs.

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.