Numpy minimum in (row, column) format

后端 未结 2 1805
独厮守ぢ
独厮守ぢ 2020-12-02 16:39

How can I know the (row, column) index of the minimum of a numpy array/matrix?

For example, if A = array([[1, 2], [3, 0]]), I want to get (1, 1)

相关标签:
2条回答
  • 2020-12-02 17:18

    [Corrected typo]

    Another simple solution is

    ri, ci = A.argmin()//A.shape[1], A.argmin()%A.shape[1]
    

    As numpy.argmin returns the index reading in row-major order


    Yes, you are right, it was a typo, which worked for square matrix

    0 讨论(0)
  • 2020-12-02 17:29

    Use unravel_index:

    numpy.unravel_index(A.argmin(), A.shape)
    
    0 讨论(0)
提交回复
热议问题