lettura simple

Transposing a Matrix in Matlab

In this lesson, I will explain how to transpose a matrix in Matlab.

Matrix transposition is the process of swapping the rows with the columns, and vice versa. For example, consider the following matrix.
$$ M = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix} $$
The transpose of a matrix is obtained by writing the elements of each row in a column, and vice versa.
$$ M^T = \begin{pmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{pmatrix} $$

Let's look at a practical example.

Create a 2x3 rectangular matrix.

>> M = [ 1 2 3 ; 4 5 6 ]
M =
1 2 3
4 5 6

It's a rectangular matrix with two rows and three columns:

$$ M = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{pmatrix} $$

To transpose the matrix, simply add an apostrophe after the variable name.

Type M'

>> M'
ans =
1 4
2 5
3 6

Alternatively, you can also use the function transpose(M) to achieve the same result.

>> transpose(M)
ans =
1 4
2 5
3 6

In both cases, the result is the same.

It's a 3x2 matrix MT with three rows and two columns:

$$ M^T = \begin{pmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{pmatrix} $$

The transpose MT of the matrix M has the rows arranged in columns, and vice versa.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin