In this python numpy program, we will sort an array by row in ascending order using NumPy.
Steps to solve the program
- Import the numpy library as np.
- Create an array using np.array().
- Sort an array by row in ascending order using np.sort().
- Print the output.
import numpy as np
x = np.array([[6, 5],
[10, 9],
[8, 7]])
print("Sorting matrix by row: ")
print(np.sort(x))
Output :
Sorting matrix by row:
[[ 5 6]
[ 9 10]
[ 7 8]]