Cara menggunakan transpose square matrix python

In this article we will discuss the steps and intuition for calculating the transpose of a matrix using Python.

Table of contents


Books I recommend:

  • Python Crash Course
  • Automate the Boring Stuff with Python
  • Beyond the Basic Stuff with Python
  • Serious Python


Introduction

Transpose 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:


pip install numpy

Transpose of a matrix explained

The 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 example

Given 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:

Cara menggunakan transpose square matrix python

Non-square matrix example

Given 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:

Cara menggunakan transpose square matrix python


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 Python

In order to create an identity matrix in Python we will use the numpy library. And the first step will be to import it:


import numpy as np

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:


A = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

print(A)

The input matrix should be:

[[1 2 3]
 [4 5 6]
 [7 8 9]]

The final step is to transpose the matrix using Python:


A_T = np.transpose(A)

print(A_T)

And you should get:

[[1 4 7]
 [2 5 8]
 [3 6 9]]

which is exactly the same answer as in the .


Conclusion

In 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 v to v_T without going back through a list of lists. I have tried to map out the relationship between the matrix position and the list indices, but I am not seeing the pattern, let alone one that would generalize to lists of any (square) length.

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 l, then it is easy to just create the transpose and unpack. But I am imagining you have the flattened matrix (v) as a starting point, and you want to compute v_T directly. So I am really more curious about that algorithm now, and how to do so in Python.