Python Practice › NumPy Programs › Program 45

Transpose a matrix

EasyNumPy Programs · 45 of 100

Write a NumPy program to swap the rows and columns of a matrix.

Advertisement
Input
[[5, 6], [7, 8], [9, 10]]
Output
[[ 5  7  9]
 [ 6  8 10]]

Approach

  1. .T.

Solution

Python
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.

More numpy programs

← All 100 NumPy Programs

Leave a Comment