- Transpose of a matrix is achieved by flipping the matrix over its main diagonal.
- Transpose of a matrix is formed in two steps. In the new matrix,
- copy the columns of the original matrix as rows.
- copy the rows of the original matrix as columns.
- By repeating the transpose operation on the already transposed matrix yields the original matrix.
- Using the transpose() method of the numpy.ndarry transpose of a matrix can be obtained.
import numpy as np
# Populate Array
for x in range(0, matrix_in.shape):
for y in range(0, matrix_in.shape):
matrix_in[x][y] = random.randrange(1, 5)
# Create a matrix1
matrix1 = np.ndarray((3, 3))
# Populate the matrix
# Create the transpose of the matrix
transposed = matrix1.transpose()
# Print the Matrices
print("Transpose of Matrix:")
[[ 2. 1. 4.]
[ 4. 4. 2.]
[ 2. 2. 3.]]
Transpose of Matrix:
[[ 2. 4. 2.]
[ 1. 4. 2.]
[ 4. 2. 3.]]