vidya nair
vidya nair

Reputation: 61

function to find minimum number greater than zero from rows of a array and store into a list

I want to find out minimum number from the rows of 9x9 array A and store in a list m. But I want to exclude zero. The program that I made is returning zero as minimum.

m = []
def find (L):
    for i in range(len(L)):
            m.append(A[L[i]].min()) 

c = [2,3,4,5,6];
find(c)
print m

Upvotes: 3

Views: 1453

Answers (1)

Divakar
Divakar

Reputation: 221554

Here's a NumPy solution -

np.where(a>0,a,a.max()).min(1)

Sample run -

In [45]: a
Out[45]: 
array([[0, 4, 6, 6, 1],
       [3, 1, 5, 0, 0],
       [6, 3, 6, 0, 0],
       [0, 6, 3, 5, 2]])

In [46]: np.where(a>0,a,a.max()).min(1)
Out[46]: array([1, 1, 3, 2])

If you want to perform this operation along selected rows only specified by row indices in L -

def find(a,L): 
    return np.where(a[L]>0,a[L],a.max()).min(1)

Sample run -

In [62]: a
Out[62]: 
array([[0, 4, 6, 6, 1],
       [3, 1, 5, 0, 0],
       [6, 3, 6, 0, 0],
       [0, 6, 3, 5, 2]])

In [63]: L = [2,3]

In [64]: find(a,L)
Out[64]: array([3, 2])

Upvotes: 6

Related Questions