Python Program to Transpose a Matrix

Posted in

Vinay Khatri
Last updated on August 10, 2022

There are several operations that we can perform on matrices. The transposition of a matrix is one such operation. The resulting matrix has interchanged rows and columns of the original one.

Here in this article, we have provided a python Program to transpose a matrix with source code/write a program to find the transpose of a matrix in python.

Matrix and Transpose

A matrix is a 2-dimensional array, which is formed by using columns and rows. Matrices are very useful in scientific computation. In Python, to represent a matrix, we use a list of lists . To transpose a matrix, we switch its row values with the column values.

``````For instance,

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

is a matrix, and for transposing it, we switch its row values with its column values i.e.:

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

Steps for Transposing a Matrix

• Ask the user to enter the number of rows and columns for a matrix.
• Then using the list comprehension and input() function, ask the user to enter the elements in the matrix.
• Create a result matrix, whose dimensions are the invert of the user-entered matrix dimensions. For instance, if the matrix has 2 rows and 3 columns then the resulting matrix (transpose) should have 3 rows and 2 columns.
• Using the nested for loop, we will change the row values with the column values then store them into the resulting matrix.

Python Program to Transpose a Matrix with Source Code/Python Transpose

Source Code

``````rows = int(input("Enter the Number of rows : " ))
column = int(input("Enter the Number of Columns: "))

print("Enter the elements of Matrix:")
matrix= [[int(input()) for i in range(column)] for i in range(rows)]
print("-------Your  Matrix is---------")
for n in matrix:
print(n)

#result matrix of column*row  dimension

result =[[0 for i in range(rows)] for j in range(column)]

#transpose the matrix
for r in range(rows):

for c in range(column):
#here we are grabbing the row data of matrix and putting it in the column on the result
result[c][r] = matrix[r][c]

print("Transpose matrix is: ")
for r in result:
print(r)``````

Output 1:

``````Enter the Number of rows : 3
Enter the Number of Columns: 3
Enter the elements of Matrix:
1
2
3
4
5
6
7
8
9
-------Your Matrix is---------
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Transpose matrix is:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]``````

Output 2

``````Enter the Number of rows : 2
Enter the Number of Columns: 4
Enter the elements of Matrix:
1
2
3
4
5
6
7
8
-------Your Matrix is---------
[1, 2, 3, 4]
[5, 6, 7, 8]
Transpose matrix is:
[1, 5]
[2, 6]
[3, 7]
[4, 8]``````

Conclusion

That was a simple Python program to generate the transpose of a matrix. It is just one of the many operations that we can perform on matrices. These include addition, subtraction, and multiplication. Matrices are very useful in programming, so building a good grasp of them is important.

People are also reading:

Tags:
python program