hulkinBrain - 1 month ago 5
Python Question

# Python alternative for MATLAB code 'min(Ar_1(Ar_1~=0))'

I want to achieve the same result with least complexity in python as

`min(Ar(Ar~=0))`
in MATLAB where
`Ar`
is a 2D numpy array.

For those who are not familiar with MATLAB,
`~=`
means
`!=`
or not equal to.

Is there a function in python which returns the indexes of the elements:

1. Whose values fulfill a condition (elements which are != 0 in this case)

2.
Which can directly be used as list index input for another array? (As
`(Ar~=0)`
's result is being used as an input like this
`Ar(Ar~=0)`

Here
`Ar~=0`
has been used as list index input like this
`Ar(Ar~=0)`
and then min of the array
`Ar(Ar~=0)`
is being found out. In other words minimum value of the array is found out excluding the elements whose value is 0.

The python syntax for a numpy array A would be:

``````A[A!=0].min()
``````

you can also set the array elements:

``````B = A.copy()
B[A==0] = A[!=0].min()
``````

just as an example setting a cutoff