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.]])
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.
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 .
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.
numpy. amax() will find the max value in an array, and numpy. amin() does the same for the min value.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With