0

I'm trying to print the index of the smallest number in an array. The array is:

import numpy as np

z = np.array(
    [
        [2.3, 5.9, 0.01],
        [7.7, 2.22, 1.02],
        [0.5, 2.7, 9.8],
        [3.76, 5.323, 5.24],
    ]
)

so the smallest value is 0.01. But when I print the index of this:

new = np.argmin(z)

My output is 2 which is only the index for column. However, I also want the index for the row which is 0. Is there an easy way to do this?

Alex
  • 6,610
  • 3
  • 20
  • 38
Angel
  • 31
  • 5

1 Answers1

3

There is probably an easier way, but this works:

x=np.unravel_index(np.argmin(z), z.shape, order='C')
print(x)

output:

(0,2) #row, column
joostblack
  • 2,465
  • 5
  • 14