Python Program To Transpose A Matrix

3 min read

Matrices are a multidimensional array of order m*n where m is the number of rows and n number of columns. Matrices are handy to store data.

A  matrix can be represented in Python row-wise, as a list of lists where each inner list represents one row of the matrix. For instance, the matrix

  1  2  3
  4  5  6

would be represented as M =  [[1, 2, 3], [4, 5, 6]]. Here, M[0] will return elements of the first row and M[0][0] will give the first element of the first row and so on, note that similar to arrays indexing in matrix starts with 0.

Matrix created as a result of interchanging the rows and columns of a matrix is called Transpose of that Matrix, for instance, the transpose of the above matrix would be:

1  4
2  5
3  6

This transposed matrix can be written as [[1, 4], [2, 5], [3, 6]]. In Python, there is always more than one way to solve any problem.

In this article, we will learn how to programmatically find out the transpose of any given matrix.

Python Program To Transpose a Matrix With List Comprehension

def transpose(m):
    rez = [[[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]]
    for row in rez:
        return row
print("result=",transpose([[1, 3, 5], [2, 4, 6]]))
print("result=",transpose([[1, 1, 1], [2, 2, 2], [3, 3, 3]]))
print("result=",transpose([[1, 4, 9]]))

Output:

result= [[1, 2], [3, 4], [5, 6]]
result= [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
result= [[1], [4], [9]]

The above program is using list comprehension to transpose the matrix, the list evaluating from the expression is the transpose of the given matrix.

Python Program To Transpose a Matrix Using Zip

Python comes with many inbuilt libraries zip is among those. The zip() function returns an iterator of tuples based on the iterable object. In order to get the transpose of the matrix first, we need to unzip the list using * operator then zip it.

def transpose(m):
    t_matrix = [*zip(*m)]
    return t_matrix


print("result=", transpose([[1, 3, 5], [2, 4, 6]]))
print("result=", transpose([[1, 1, 1], [2, 2, 2], [3, 3, 3]]))
print("result=", transpose([[1, 4, 9]]))

Output:

result= [(1, 2), (3, 4), (5, 6)]
result= [(1, 2, 3), (1, 2, 3), (1, 2, 3)]
result= [(1,), (4,), (9,)]

Python Program To Transpose a Matrix Using NumPy

NumPy is an extremely popular library among data scientist heavily used for large computation of array, matrices and many more with Python.

NumPy comes with an inbuilt solution to transpose any matrix numpy.matrix.transpose the function takes a numpy array and applies the transpose method.

Note that, Numpy doesn't come bundled with default Python installation, so you need to install it first. NumPy can be installed using the pip installer as follows.

pip install numpy

Once the installation is done executing the following program will return the transpose of a given matrix,

import numpy

def transpose(m):
    m = numpy.array(m)
    return m.transpose()

print("result: \n", transpose([[1, 3, 5], [2, 4, 6]]))
print("result: \n", transpose([[1, 1, 1], [2, 2, 2], [3, 3, 3]]))
print("result: \n", transpose([[1, 4, 9]]))

Output:

result:
 [[1 2]
 [3 4]
 [5 6]]
result:
 [[1 2 3]
 [1 2 3]
 [1 2 3]]
result:
 [[1]
 [4]
 [9]]

 


PROGRAMS

Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read