0

For example I have the following 2D array:

array([[ 5.59635947,  1.42474555,  1.56519762],
       [ 6.16476541,  6.12324772,  5.32735447],
       [ 8.60258444,  7.16592582,  1.49555662],
       [ 0.63983973,  5.50249666,  3.52246942],
       [ 5.34002941,  4.87065573,  9.80725886]])

I use argmax along axis= 0 to get the position of maximum element in each column:

i = np.argmax(c,axis=0)
array([2, 2, 4])

Well, I have already calculated the indices. How can I get maximum element of each column?

When I write the following code:

dd_m= np.zeros(i.shape)
for k in np.arange(i.size):
    dd_m[k] = c[i[k],k]

I get right answer, which is:

 array([ 8.60258444,  7.16592582,  9.80725886])

Is there's more efficient or pythonic way to do this?

Krishi H
  • 526
  • 8
  • 26

1 Answers1

2

Try:

c.max(axis=0)

Or:

np.max(c, axis=0)

Or if you have already calculated the indices:

c[i, np.arange(c.shape[1])]

Where c is your matrix, i is the found indices of max values.

Fomalhaut
  • 8,590
  • 8
  • 51
  • 95