1
如何找到滿足多個條件的Numpy數組中的元素索引?查找具有多個條件的numpy數組中的元素
例如:功能numpy.nonzero
讓我根據一些標準查找指數:
In [1]: from numpy import *
In [2]: a = array([1,0,1,-1])
In [5]: nonzero(a != 0)
Out[5]: (array([0, 2, 3]),)
但是,讓這樣的多重標準不起作用:
In [6]: nonzero((a != 0) and (a < 0))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/Users/cls/<ipython-input-6-85fafffc5d1c> in <module>()
----> 1 nonzero((a != 0) and (a < 0))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
在MATLAB中,有可能寫:
find((d != 0) & (d < 0))
我怎樣才能做到這一點與NumPy的?
感謝。 「IIUC」是什麼意思? – clstaudt
「如果我理解正確。」 – DSM