Numpy minimum in (row, column) format

走远了吗. 提交于 2019-12-17 15:35:35

问题


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)

Thanks!


回答1:


Use unravel_index:

numpy.unravel_index(A.argmin(), A.shape)



回答2:


[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



来源:https://stackoverflow.com/questions/3230067/numpy-minimum-in-row-column-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!