How to inverse a vector in MATLAB?


As we know, MATLAB is a powerful tool to perform various operations on matrices and arrays. Vector is a type of matrix having only one row or one column. We can determine the inverted version of a vector using MATLAB. For this, MATLAB provides different methods and functions. So, let’s discuss the process of inverting a vector using different methods in MATLAB.

Inverse a Vector Using Indexing Method in MATLAB

In MATLAB, we can utilize the indexing method to find inverse of a given vector. In this method, the elements of the vector are arranged in the reverse order using their indexing.

Example

Here is an example to explain how the indexing method is used to find the inverse of a vector.

% MATLAB code to inverse a vector using indexing method
% Create a sample vector
V = [12, 20, 31, 14, 50, 14, 30, 10, 11, 9];

% Find the inverse of the vector
I = V(end : -1 : 1);

% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);

Output

When you run this code, it will produce the following output

The input vector is:
    12    20    31    14    50    14    30    10    11     9

The inverted vector is:
     9    11    10    30    14    50    14    31    20    12

Explanation

In this example, we start by creating a sample vector "V". Then, we find the inverse of the vector by reversing the indexing of elements of the vector, and store the inversion result in a variable "I". Finally, we display the input and inverted vectors using the "disp" function.

Inverse a Vector Using "flip" Function in MATLAB

In MATLAB, there is a built-in function named, "flip" that can be used to find the inversion of a vector. This function arranges the elements of the input vector in the reverse order.

However, the "flip" function is available in MATLAB R2019b or later versions of MATLAB.

Syntax

I = flip(V);

Here, "V" is the input vector and "I" is the inverted vector.

Example

Let us see an example to understand the implement this method of finding inverse of a vector.

% MATLAB code to inverse a vector using flip function
% Create a sample vector
V = [12, 20, 31, 14, 50, 14, 30, 10, 11, 9];

% Fin inverse of the vector using the flip function
I = flip(V);

% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);

Output

When you run this code, it will produce the following output

The input vector is:
    12    20    31    14    50    14    30    10    11     9

The inverted vector is:
     9    11    10    30    14    50    14    31    20    12

Explanation

In this MATLAB example, firstly we create a sample vector. Then we call the "flip" function with "V" as the input vector, and store the output inverted vector in the variable "I". Finally, we display the input and inverted vectors using the "disp" function.

Inverse a Vector Using "fliplr" Function in MATLAB

In MATLAB, the "fliplr" function is used to find the inverse of a row vector. In other words, the "fliplr" function reverses the order of elements of a vector along the row.

Syntax

I = fliplr(V);

Example

The following MATLAB example demonstrates the use of the "fliplr" function to find inverse of a row vector.

% MATLAB code to inverse a vector using the fliplr function
% Create a sample vector
V = [12, 20, 31, 14, 50, 14, 30, 10, 11, 9];

% Find the inverse of the vector
I = fliplr(V);

% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);

Output

When you run this code, it will produce the following output

The input vector is:
    12    20    31    14    50    14    30    10    11     9

The inverted vector is:
     9    11    10    30    14    50    14    31    20    12

Explanation

The code implementation and execution of this MATLAB program is same as the above codes. In this code, we have used the "fliplr" function to perform the inversion of the vector along the row.

These methods explained above are used to find the inverse of a row vector. Let us now understand how to find inverse of a column vector.

Inverse a Column Vector Using "flip" Function in MATLAB

We can also use the "flip" function to find the inverse of a column vector in MATLAB. The implementation of code is similar to that is for row vector.

Example

The following example program shows how to find the inverse of a column vector using the "flip" function.

% MATLAB code to inverse a column vector using flip function
% Create a sample column vector
V = [12; 20; 31; 14; 50];

% Fin inverse of the column vector using the flip function
I = flip(V);

% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);

Output

When you run this code, it will produce the following output

The input vector is:
    12
    20
    31
    14
    50
The inverted vector is:
    50
    14
    31
    20
    12

Inverse a Column Vector using Indexing Method in MATLAB

Let us see how we can use the indexing method to find the inverse of a column vector using MATLAB.

Example

The following example explains the indexing method to find the inverse of a column vector in MATLAB.

% MATLAB code to inverse a column vector using indexing method
% Create a sample column vector
V = [12; 20; 31; 14; 50];

% Find the inverse of the vector
I = V(end : -1 : 1);

% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);

Output

When you run this code, it will produce the following output

The input vector is:
    12
    20
    31
    14
    50
The inverted vector is:
    50
    14
    31
    20
    12

Inverse a Column Vector Using "flipud" Function in MATLAB

In MATLAB, there is a built-in function "flipud" that is used to find the inverse of a column vector.

Syntax

I = flipud(V);

Example

The following example program shows how to find the inverse of a column vector using the "flipud" function.

% MATLAB code to inverse a column vector using flipud function
% Create a sample column vector
V = [12; 20; 31; 14; 50];

% Fin inverse of the column vector using the flipud function
I = flipud(V);

% Display the input and inverted vectors
disp('The input vector is:');
disp(V);
disp('The inverted vector is:');
disp(I);

Output

When you run this code, it will produce the following output

The input vector is:
    12
    20
    31
    14
    50
The inverted vector is:
    50
    14
    31
    20
    12

Conclusion

This is all about finding the inverse of a vector using MATLAB. In MATLAB, there are two methods namely, indexing method and built-in function method to find the inverse of a vector. In this tutorial, I explained how to use MATLAB to find the inverse of a row or column vector with the help of examples.

Updated on: 06-Oct-2023

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements