How to Transpose Matrix in MATLAB: Syntax and Examples
In MATLAB, you can transpose a matrix using the
' operator or the transpose() function. For example, B = A' flips rows and columns of matrix A to create B.Syntax
The basic syntax to transpose a matrix A in MATLAB is:
B = A': This uses the transpose operator to flip rows and columns.B = transpose(A): This function returns the transpose ofA.
Both produce the same result for numeric matrices.
matlab
B = A';
% or equivalently
B = transpose(A);Example
This example shows how to transpose a 2x3 matrix into a 3x2 matrix using both methods.
matlab
A = [1 2 3; 4 5 6]; B1 = A'; B2 = transpose(A); B1 B2
Output
B1 =
1 4
2 5
3 6
B2 =
1 4
2 5
3 6
Common Pitfalls
One common mistake is confusing the transpose operator ' with the complex conjugate transpose. For real numbers, they are the same, but for complex numbers, ' also takes the complex conjugate.
To get the pure transpose without conjugation for complex matrices, use transpose() function.
matlab
A = [1+1i, 2+2i; 3+3i, 4+4i]; B_conj = A'; % Complex conjugate transpose B_pure = transpose(A); % Pure transpose B_conj B_pure
Output
B_conj =
1.0000 - 1.0000i 3.0000 - 3.0000i
2.0000 - 2.0000i 4.0000 - 4.0000i
B_pure =
1.0000 + 1.0000i 3.0000 + 3.0000i
2.0000 + 2.0000i 4.0000 + 4.0000i
Quick Reference
Remember these tips when transposing matrices in MATLAB:
- Use
'for transpose; it also conjugates complex numbers. - Use
transpose()for pure transpose without conjugation. - Transpose flips rows and columns, changing matrix shape.
Key Takeaways
Use
' operator or transpose() function to transpose matrices in MATLAB.' operator performs complex conjugate transpose for complex matrices.Use
transpose() for pure transpose without conjugation on complex data.Transposing flips rows and columns, changing matrix dimensions.
Check matrix type to avoid unexpected conjugation effects.