hulkinBrain - 8 months ago 33

Python Question

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

`min(Ar(Ar~=0))`

`Ar`

For those who are not familiar with MATLAB,

`~=`

`!=`

Which can directly be used as list index input for another array?

`(Ar~=0)`

`Ar(Ar~=0)`

Here

`Ar~=0`

`Ar(Ar~=0)`

`Ar(Ar~=0)`

Answer

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

Source (Stackoverflow)