askvity

How to Write Transpose of a Matrix in MATLAB?

Published in MATLAB Matrix Transpose 4 mins read

To transpose a matrix in MATLAB, you use specific operators that swap the rows and columns. The choice of operator depends on whether your matrix contains complex numbers and if you need to perform a complex conjugation during the transpose operation.

You can transpose a matrix in MATLAB using either the apostrophe operator (') or the dot-apostrophe operator (.') depending on whether you need complex conjugation.

Understanding Transpose Operators in MATLAB

MATLAB provides two primary operators for transposing matrices, each serving a slightly different purpose, particularly when dealing with complex numbers.

The Complex Conjugate Transpose (')

MATLAB uses the apostrophe operator ( ' ) to perform a complex conjugate transpose. This is the standard transpose operation often denoted as $A^H$ or $A^*$ in linear algebra literature for complex matrices. It performs two actions simultaneously:

  1. Swaps the rows and columns.
  2. Takes the complex conjugate of each element (i.e., changes the sign of the imaginary part).

Example:

A = [1 + 2i, 3; 4, 5 - 6i];
A_conj_transpose = A'; % Using the apostrophe operator
disp('Complex Conjugate Transpose (A''):');
disp(A_conj_transpose);

This code will output:

Complex Conjugate Transpose (A'):
   1.0000 - 2.0000i   4.0000
   3.0000             5.0000 + 6.0000i

Notice how the rows and columns are swapped, and the imaginary parts of the complex numbers (1+2i and 5-6i) have their signs flipped in the result.

The Non-Conjugate Transpose (.')

For situations where you only need to swap rows and columns without performing complex conjugation, MATLAB uses the dot-apostrophe operator ( . ' ) to transpose without conjugation. This is often referred to as the "plain transpose" or "array transpose" and is equivalent to the mathematical notation $A^T$.

Example:

A = [1 + 2i, 3; 4, 5 - 6i];
A_plain_transpose = A.'; % Using the dot-apostrophe operator
disp('Non-Conjugate Transpose (A.''):');
disp(A_plain_transpose);

This code will output:

Non-Conjugate Transpose (A.'):
   1.0000 + 2.0000i   4.0000
   3.0000             5.0000 - 6.0000i

Here, the rows and columns are swapped, but the complex numbers (1+2i and 5-6i) retain their original imaginary parts.

When Are the Operators the Same? (Real Matrices)

The distinction between the apostrophe (') and the dot-apostrophe (.') operators is only relevant when your matrix contains complex numbers.

As highlighted in the reference, "For matrices containing all real elements, the two operators return the same result." This is because the complex conjugate of a real number is the number itself (since there's no imaginary part to conjugate).

Example:

B = [1, 2, 3; 4, 5, 6]; % A matrix with real numbers
B_transpose1 = B';   % Using apostrophe
B_transpose2 = B.';  % Using dot-apostrophe

disp('Transpose of real matrix using '':');
disp(B_transpose1);
disp('Transpose of real matrix using .'':');
disp(B_transpose2);

Both B_transpose1 and B_transpose2 will produce the same output:

Transpose of real matrix using ':
     1     4
     2     5
     3     6

Transpose of real matrix using .':
     1     4
     2     5
     3     6

Summary Table

Here's a quick overview of the two transpose operators in MATLAB:

Operator Description Handles Complex Numbers? Mathematical Notation (Complex) Mathematical Notation (Real)
' Complex Conjugate Transpose Yes (conjugates elements) $A^H$ or $A^*$ $A^T$
.' Non-Conjugate Transpose No (does not conjugate elements) $A^T$ $A^T$

In summary, use .' for a simple row-column swap (plain transpose) and '' for a transpose that also conjugates complex elements. For real matrices, either operator will give you the standard transpose.

Related Articles