How to take Matlab Transpose – Syntax and Examples

This tutorial is about the Matlab Transpose operator. Let’s first understand the term ‘transpose’. The transpose of a matrix is an operator which switches rows with columns and vice versa. It can be also applied to a row vector or a column vector. Using the transpose operator, we can convert a row vector into a column vector and a column vector into a row vector. Suppose we have a row vector x = [1 7 3].

On applying the transpose operator, it becomes a column vector. If the transpose operator is applied again, then this column vector becomes a row vector. Similarly, when the transpose is applied to the matrix, the elements are flipped over its diagonals. Consider a 3 X 3 matrix A which is equal to:

Applying transpose on matrix A gives a 3 x 3 output matrix (O)which is produced by interchanging rows into columns and interchanging columns into rows.

In Matlab, there are three ways to take the transpose of a matrix which are:

  • Dot operator
  • using transpose() function
  • using for loop
Method 1: Using Dot Operator

The first method uses a dot operator (.’) to find the transpose. The syntax is given below:

Output = Input.'

The operator when applied on input return a resultant output vector or matrix whose number of rows is equal to the number of columns of the input vector or matrix and the number of columns is equal to the number of rows in the input vector or matrix.

Example 1:

Output:

For a vector or matrix with complex numbers, the (‘) operator interchanges rows with columns and generate the complex conjugate vector or a matrix in which the sign of the imaginary part of each complex element changes. Let’s understand this by a simple example.

Example 2:

Consider the complex matrix A.

Output:

You can observe from the output on applying (‘) operator on the matrix A, the signs of imaginary parts of all the elements are reversed. If you only want to transpose of a complex matrix, then use (‘) this operator along with a dot operator. Consider the same example.

Example 3:

Output:


Observing the output only rows and columns are interchanged. The sign of imaginary parts remains the same as in the input matrix. The (.) operator is the simplest and easiest approach to find the transpose but there are high chances of syntax error in using this operator

Method 2: Using Transpose function

The MATLAB has a built-in transpose function which when applied on any vector or matrix, it directly computes and returns the transpose of a matrix. The syntax is given below.

output vector/matrix = transpose(input vector/matrix)

The function takes an input vector or a matrix as an argument and returns the transpose.

Example 4:

Output:

There is another built-in function “ctranspose” which returns the complex conjugate transpose of a matrix.

Example 5:

Output:

Method 3: Using for loop

The third method of taking transpose is by using for loop. Create and initialize a matrix [Aij] whose transpose is to be calculated. Then create another zero matrix of size [Bji] to store the transpose of matrix A. Write a code of for loop which will iterate through rows and columns one by one and will replace the contents of Bji with Aij.

The following example demonstrates how using for-loop, you can find the transpose.

Output:

In the above example, you will observe that the matrix ‘res’ is initialized by first calculating the size of the input matrix i.e., x using the command “size(x)”. The size(x) command returns the number of rows and number of columns of matrix ‘x’ where variable ‘m’ represents a number of rows and variable ‘n’ indicates the number of columns. Then ‘res’ matrix is created by passing variable ‘n’ in a number of rows and ‘m’ in a number of columns. Then the for loop iterates through rows and columns and inserts the values.

In this article, we have seen how to find out the transpose of a vector or matrix by using different methods. Let us know if you have any queries regarding this topic. Your review matters to us a lot. Please let us know your feedback in the comments.

Leave a Comment

Your email address will not be published.