Find the min/max excluding zeros in a numpy array (or a tuple) in python

前端 未结 6 819
野性不改
野性不改 2020-12-08 09:36

I have an array. The valid values are not zero (either positive or negetive). I want to find the minimum and maximum within the array which should not take zeros into accoun

6条回答
  •  鱼传尺愫
    2020-12-08 09:43

    How about:

    import numpy as np
    minval = np.min(a[np.nonzero(a)])
    maxval = np.max(a[np.nonzero(a)])
    

    where a is your array.

提交回复
热议问题