Python Practice › NumPy Programs › Program 45
Transpose a matrix
Write a NumPy program to swap the rows and columns of a matrix.
Input
[[5, 6], [7, 8], [9, 10]]
Output
[[ 5 7 9] [ 6 8 10]]
Approach
.T.
Solution
import numpy as npprint(np.array([[5, 6], [7, 8], [9, 10]]).T)Output
[[ 5 7 9] [ 6 8 10]]
Learn the concept
New to this topic? Read the lesson, then come back.