Active July 21, 2020    /    Viewed 4298    /    Comments 0    /    Edit

Examples of how to transpose (inverse columns and rows) a matrix using numpy in python:

Matrix transpose

Let's consider the following matrix:

\begin{equation}
M = \left( \begin{array}{ccC}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{array}\right)
\end{equation}

then, the matrix transpose is:

\begin{equation}
M^T = \left( \begin{array}{ccC}
1 & 4 & 7 \\
2 & 5 & 8 \\
3 & 6 & 9
\end{array}\right)
\end{equation}

Transpose a matrix using numpy (method 1)

>>> import numpy as np
>>> M = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> M
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> M.T
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])

Transpose a matrix using numpy (method 2)

>>> np.transpose(M)
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])  Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!

Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount