MATLAB - Transpose Operator
MATLAB - Matrix Transpose
Section titled “MATLAB - Matrix Transpose”The transpose is a fundamental matrix operation that swaps a matrix’s rows with its columns. It’s widely used in linear algebra, data analysis, and many scientific computing tasks. This tutorial covers how to perform transposition in MATLAB, highlighting a critical distinction when working with complex numbers.
Understanding Matrix Transpose
Section titled “Understanding Matrix Transpose”The transpose of a matrix A is denoted as AT. If A is an m x n matrix, then AT will be an n x m matrix. The element at row i, column j in the original matrix moves to row j, column i in the transposed matrix.
For example, consider the following 3x3 matrix M:
M = 1 2 3 4 5 6 7 8 9The transpose of matrix M, denoted M_T, would be:
M_T = 1 4 7 2 5 8 3 6 9An important property is that transposing a matrix twice returns the original matrix: (AT)T = A.
Transpose Operators in MATLAB
Section titled “Transpose Operators in MATLAB”MATLAB provides two main operators for transposition, and it’s crucial to understand their difference:
.'(dot-apostrophe): Performs the standard, non-conjugate transpose.'(apostrophe): Performs the complex conjugate transpose (also known as the Hermitian transpose or adjoint).
Transposing Real Matrices
Section titled “Transposing Real Matrices”For matrices containing only real numbers, both operators (.' and ') produce the same result.
%% Create a real matrixrealMatrix = [1, 2, 3; 4, 5, 6; 7, 8, 9];
%% Using the non-conjugate transpose operator (.')transpose1 = realMatrix.';
%% Using the conjugate transpose operator (')transpose2 = realMatrix';
% Display the resultsdisp('Original Matrix:');disp(realMatrix);disp('Transpose using .'':');disp(transpose1);disp('Transpose using '':'');disp(transpose2);Executing this code will show that transpose1 and transpose2 are identical:
Original Matrix: 1 2 3 4 5 6 7 8 9
Transpose using .': 1 4 7 2 5 8 3 6 9
Transpose using ': 1 4 7 2 5 8 3 6 9Transposing Complex Matrices: The Critical Difference
Section titled “Transposing Complex Matrices: The Critical Difference”The difference between the two operators becomes apparent when working with complex numbers. The complex conjugate of a number a + bi is a - bi.
A.'(Non-conjugate transpose) simply swaps rows and columns.A'(Complex conjugate transpose) swaps rows and columns AND takes the complex conjugate of each element.
%% Create a complex matrixcomplexMatrix = [1+2i, 3-4i; 5, 6+7i];
%% Perform non-conjugate transposenonConjugateT = complexMatrix.';
%% Perform complex conjugate transposeconjugateT = complexMatrix';
% Display the resultsdisp('Original Complex Matrix:');disp(complexMatrix);disp('Non-Conjugate Transpose (. ''):');disp(nonConjugateT);disp('Complex Conjugate Transpose (''):');disp(conjugateT);The output clearly shows the difference:
Original Complex Matrix: 1.0000 + 2.0000i 3.0000 - 4.0000i 5.0000 + 0.0000i 6.0000 + 7.0000i
Non-Conjugate Transpose (. '): 1.0000 + 2.0000i 5.0000 + 0.0000i 3.0000 - 4.0000i 6.0000 + 7.0000i
Complex Conjugate Transpose ('): 1.0000 - 2.0000i 5.0000 + 0.0000i 3.0000 + 4.0000i 6.0000 - 7.0000iBest Practices and Common Pitfalls
Section titled “Best Practices and Common Pitfalls”Pro Tip: Unless you specifically require the complex conjugate transpose for a mathematical algorithm (e.g., in linear algebra for Hermitian matrices), it is a modern best practice to default to the dot-transpose operator (.).
This prevents subtle bugs if your code unexpectedly receives complex data, for example, from a Fast Fourier Transform (FFT).
Function-Based Transposition
Section titled “Function-Based Transposition”MATLAB also provides functions that mirror the behavior of the operators. While operators are generally more idiomatic and common in MATLAB code, these functions can be useful when passing an operation as a function handle.
transpose(A): Equivalent toA.'(non-conjugate).ctranspose(A): Equivalent toA'(complex conjugate).
%% Using the transpose() functionfuncTranspose = transpose(complexMatrix);disp('Result from transpose() function:');disp(funcTranspose);
%% Using the ctranspose() functionfuncCTranspose = ctranspose(complexMatrix);disp('Result from ctranspose() function:');disp(funcCTranspose);The output of this code will match the respective operator-based results shown previously.