I want to find the indices of maximum and minimum elements in each row and column of a matrix. What Python function should I use for it?

+1 vote

Best answer

You can use Numpy's **argmax()** / **argmin()** functions that return the indexes of the first occurrences of the maximum/minimum values along the specified axis.

Here is an example:

>>> import numpy as np

>>> aa=np.matrix([[3,1,4,6],[12,45,6,9],[89,0,5,19]])>>> aa.argmin(axis=0)

matrix([[0, 2, 0, 0]])>>> aa.argmin(axis=1)

matrix([[1],

[2],

[1]])>>> aa.argmax(axis=0)

matrix([[2, 1, 1, 2]])>>> aa.argmax(axis=1)

matrix([[3],

[1],

[0]])