find function matlab in numpy/scipy

前端 未结 3 622
不思量自难忘°
不思量自难忘° 2021-02-15 06:09

Is there an equivalent function of find(A>9,1) from matlab for numpy/scipy. I know that there is the nonzero function in numpy but what I need is th

3条回答
  •  死守一世寂寞
    2021-02-15 07:17

    matlab's find(X, K) is roughly equivalent to numpy.nonzero(X)[0][:K] in python. @Pavan's argmax method is probably a good option if K == 1, but unless you know apriori that there will be a value in A >= 9, you will probably need to do something like:

    idx = (A >= 9).argmax()
    if (idx == 0) and (A[0] < 9):
        # No value in A is >= 9
        ...
    

提交回复
热议问题