Python Program to Transpose a Matrix
The transpose of a matrix is a new matrix that is obtained by interchanging its rows and columns. The transpose of a matrix is denoted by placing a superscript “T” after the original matrix name.
For example, consider the following matrix A:
A = [ 1 2 3 4 5 6 7 8 9 ]
The transpose of matrix A is denoted by A^T and is obtained by interchanging its rows and columns, resulting in:
A^T = [ 1 4 7 2 5 8 3 6 9 ]
The transpose of a matrix can be useful in various ways. For example, it can be used to find the dot product of two matrices, or to convert a row vector into a column vector, or vice versa. It is also used in the process of solving systems of linear equations and in other areas of mathematics and engineering.
Python Code :
The below Python program to transpose a matrix:
# Define a matrix A A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Create a matrix to hold the transpose of A transpose = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] # Transpose the matrix A and store the result in transpose for i in range(len(A)): for j in range(len(A)): transpose[j][i] = A[i][j] # Display the resulting transposed matrix for row in transpose: print(row)
The matrix A is defined as a 3x3 list of lists.
A matrix transpose is created as a 3x3 list of lists initialized to all zeros.
The matrix A is transposed by iterating over its rows and columns using a nested for loop. The element at position (i, j) in the original matrix A is moved to position (j, i) in the transposed matrix.
The resulting transposed matrix is displayed using a for loop that iterates over the rows of the matrix and prints each row.
[1, 4, 7] [2, 5, 8] [3, 6, 9]