find function matlab in numpy/scipy

前端 未结 3 614
不思量自难忘°
不思量自难忘° 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:10

    The equivalent of find in numpy is nonzero, but it does not support a second parameter. But you can do something like this to get the behavior you are looking for.

    B = nonzero(A >= 9)[0] 
    

    But if all you are looking for is finding the first element that satisfies a condition, you are better off using max.

    For example, in matlab, find(A >= 9, 1) would be the same as [idx, B] = max(A >= 9). The equivalent function in numpy would be the following.

    idx = (A >= 9).argmax()
    

提交回复
热议问题