Home > front end >  Find 2nd minimum along second axis in a 2-D numpy array
Find 2nd minimum along second axis in a 2-D numpy array

Time:12-20

I have a numpy array like this

[[ 1, 5, 9, 7],
 [ 5, 8, 9, 7],
 [-9, 6, 2, 3]]

I want second minimum from every array present in the 2D array like this

[5,7,2]

CodePudding user response:

Actually, you want the second minimum from each row of the source (2-D) array.

To get it, just sort the source array along axis=1 and then take the second column (numbering from 0, so the column number is actually 1):

result = np.sort(arr, axis=1)[:,1]

The result is:

array([5, 7, 2])
  • Related