In this article we will discuss the steps and intuition for calculating the transpose of a matrix using Python. Show Table of contents Books I recommend:
IntroductionTranspose of a matrix is one of the fundamental topics in linear algebra, and also one of the simplest ones to understand. To continue following this tutorial we will need the following Python library: numpy. If you don’t have them installed, please open “Command Prompt” (on Windows) and install them using the following code:
Transpose of a matrix explainedThe transpose of a matrix, in linear algebra, is an operator which rotates the matrix around its main diagonal. In simple words, the rows become columns, and columns become rows of a matrix. Square matrix exampleGiven some matrix \(A\): $$A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}$$ Its transpose is: $$A^T = \begin{bmatrix} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{bmatrix}$$ The steps of transposition are quite simple. First we find the main diagonal (highlighted in red below), and then we essentially flip the matrix around it, and that’s how we get the transpose: Non-square matrix exampleGiven some matrix \(A\) of \(3 \times 2\) dimension (can be any other dimension): $$A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{bmatrix}$$ Its transpose is a matrix of \(2 \times 3\) dimension (in this case): $$A^T = \begin{bmatrix} 1 & 3 & 5 \\ 2 & 4 & 6 \end{bmatrix}$$ The steps of transposition are the same as for the square matrix. First we find the main diagonal (highlighted in red below), and then we essentially flip the matrix around it, and that’s how we get the transpose: We can generalize the matrix transpose steps as: Given matrix \(A\) of \(m \times n \) dimension, it’s transpose is matrix \(A^T\) of \(n \times m\) dimension. We are essentially always rotating the matrix around the main diagonal. Note that the transpose of a diagonal matrix will be the same matrix. Transpose a matrix using PythonIn order to create an identity matrix in Python we will use the numpy library. And the first step will be to import it:
Numpy has a lot of useful functions, and for this operation we will use the transpose() function which will transpose a given matrix. Recall that in Python matrices are constructed as arrays. So the next step will be to define the input matrix:
The input matrix should be:
The final step is to transpose the matrix using Python:
And you should get:
which is exactly the same answer as in the . ConclusionIn this article we discussed the steps and intuition for calculating a transpose of a matrix using Python. Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Linear Algebra articles. My question is, is there a way to take the flattened list version of a square matrix, and rearrange it so it becomes the transposed version? Here, that would be to get from In order to try to avoid any XY problems: my original goal was to be able to take some simple list of list matrices and iterate over them in different ways (i.e. left>right then top>bottom versus top>bottom then left>right). And if your starting point is |