Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the minimum value in a numpy matrix?

Hey this is a quick and easy question... How would i find the minimum value of this matrix, excluding 0? As in, 8

arr = numpy.array([[  0.,  56.,  20.,  44.],
                   [ 68.,   0.,  56.,   8.],
                   [ 32.,  56.,   0.,  44.],
                   [ 68.,  20.,  56.,   0.]])
like image 280
Sean Avatar asked Aug 01 '12 17:08

Sean


People also ask

How do you find the minimum number in a matrix in python?

Passing a list of numbers to min(), returns the minimum value. Since, matrix is a list of lists, map() can be used to find minimum value for the each sub-list present in the matrix. Since, map returns an iterator, min can be applied again to find the resultant minimum.

How do you find the minimum value of a matrix?

M = min( A ) returns the minimum elements of an array. If A is a vector, then min(A) returns the minimum of A . If A is a matrix, then min(A) is a row vector containing the minimum value of each column of A .

How do you find the minimum value of a 2D NumPy array?

Find min values along the axis in 2D numpy array | min in rows or columns: If we pass axis=0 in numpy. amin() then it returns an array containing min value for each column i.e. If we pass axis = 1 in numpy.

How do you find min and max in NumPy?

numpy. amax() will find the max value in an array, and numpy. amin() does the same for the min value.


1 Answers

As you're using numpy, you could use

arr[arr>0].min()

for the case you posted. but if your array could have negative values, then you should use

arr[arr != 0].min()
like image 158
jmetz Avatar answered Oct 06 '22 00:10

jmetz