Turn a Matrix into a Row Vector in MATLAB


In MATLAB, a matrix is nothing but an array of rows and columns arranged in a square or rectangular shape. A matrix is basically a two−dimensional array of numbers. MATLAB allows us to create various types of matrices, such as n × n matrix, n × m matrix, column vector, row vector, etc.

In this article, we will learn how to turn a matrix into a row rector using MATLAB programming.

A row vector is a one−dimensional array in which the elements of the matrix are arranged in a single row. When we create a row vector, MATLAB stores the elements of the matrix row−wise, i.e. the elements of the each row are stored sequentially in the memory.

MATLAB provides different methods to convert any kind of matrix into a row vector. Some commonly used methods are described below.

Method (1): By Using Colon Operator and Transpose Function

In MATLAB, we can use the colon operator (:) along with the ‘transpose()’ function to convert a matrix into a row vector.

The step−by−step process to convert a matrix into a row vector using the colon operator ‘:’ and the ‘transpose’ function is explained below:

Step 1 − First of all, we use the colon operator ‘:’ to convert the given matrix into a column vector. As the ‘:’ allows to arrange all the elements of a matrix in column−wise fashion.

Step 2 − Next, we use the ‘transpose()’ function to convert the column vector into the row vector.

Now, let us consider an example program to convert a matrix into a row vector using the ‘:’ and ‘transpose()’ in MATLAB.

Example

% MATLAB program to convert a matrix into a row vector using colon operator and transpose function
% Define a 2 × 2 matrix
m = [1 2; 3 4];
% Convert the matrix into a column vector
c = m(:);
% Obtain the row vector
r = transpose(c);
% Display the result
disp('The resulting row vector is');
disp(r);

Output

The resulting row vector is
     1     3     2     4

Explanation

In this MATLAB program, we have converted a 2 × 2 matrix into a row vector by using the colon operator ‘:’ and the ‘transpose()’ function.

In this code, firstly we create a 2 × 2 matrix and store it in the ‘m’ variable. Next, we use the ‘:’ operator to convert the matrix into a column vector and store the result in the ‘c’ variable. After that we call the ‘transpose()’ function to convert the column vector into a row vector and store the result in the ‘r’ variable. Finally, we use the ‘disp’ function to display the result.

This is how, we can turn a matrix into a row vector using MATLAB programming.

Let us consider another MATLAB program example to turn a 3 × 3 matrix into a row vector using the colon operator and the transpose function.

Example

% MATLAB program to turn a matrix into a row vector 
% Define a 3 × 3 matrix
m = [1 3 5; 2 4 6; 7 8 9];
% Convert the matrix into a column vector
c = m(:);
% Obtain the row vector
r = transpose(c);
% Display the result
disp('The resulting row vector is');
disp(r);

Output

The resulting row vector is
     1     2     7     3     4     8     5     6     9

Explanation

This MATLAB code converts a 3 × 3 matrix into a row vector by using the colon operator ‘:’ and the ‘transpose’ function. The code explanation is same as the MATLAB program (1).

We can apply this code to turn a matrix of any order into a row vector. For example, you can try this code with a 4 × 4 matrix.

Now, let us discuss the second approach to turn a matrix into a row vector using MATLAB programming.

Method (2): By Using Colon Operator and Transpose Operator

We can also turn a matrix into a row vector by using the colon operator and the transpose operator. The algorithm behind this method is same as that of the colon operator with transpose function.

The following MATLAB program demonstrates the conversion of a 4 × 4 matrix into a row vector by using the colon operator ‘:’ and the transpose operator ‘'’.

Example

% MATLAB program to turn a matrix into a row vector 
% Define a 4 × 4 matrix
m = [1 3 5 0; 2 4 6 1; 7 8 9 4];
% Convert the matrix into the row vector
r = m(:)';
% Display the result
disp('The resulting row vector is');
disp(r);

Output

The resulting row vector is
     1     2     7     3     4     8     5     6     9     0     1     4

Explanation

In this MATLAB code, firstly we define a 4 × 4 matrix. Then, we use the colon operator ‘:’ and the transpose operator ‘'’ to convert the matrix into a row vector and store the result in the ‘r’ variable. Finally, we call ‘disp’ function to display the result.

Method (3): By Using the ‘reshape()’ Function

MATLAB provides a built−in function named ‘reshape()’ that we can use to turn a matrix into a row vector. This function reshape a matrix based on a specified size.

The following MATLAB program demonstrates the use of the ‘reshape’ function to convert a matrix into a row vector.

Example

% MATLAB code for turning a matrix into a row vector using reshape function
% Define a 4 × 4 matrix
m = [1 2 3 4; 5 6 7 8; 9 0 1 2];
% Identity the number of elements in the matrix
n = numel(m);
% Reshape the matrix into a row vector
r = reshape(m, 1, n);
disp('The resulting row vector is');
disp(r);

Output

The resulting row vector is
     1     5     9     2     6     0     3     7     1     4     8     2

Explanation

This MATLAB code converts the 4 × 4 matrix into a row vector by using the reshape function. In the code, we start by creating a 4 × 4 matrix and store it in a variable ‘m’. Next, we determine the number of elements in the matrix by using the ‘numel’ function and store the result in the ‘n’ variable. After that we use the ‘reshape’ function with a size parameter ‘1’ to convert the matrix into a row vector. Finally, we display the result by using the ‘disp’ function.

Conclusion

Hence, in this article, we have covered various methods to convert a matrix into a row vector using MATLAB programming. MATLAB allows use to convert a given matrix into a row vector simply by writing simple MATLAB codes. In the above sections, we have mentioned multiple MATLAB programs, highlighting different approaches to turn a matrix into a row vector.

Updated on: 08-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements