0
我有一个很大的无向数组。我想迭代它来检查一个条件是否在本地满足。下一段代码解释了我的问题。在多维numpy数组上迭代的快速条件检查
a = np.random.randint(2, size=(60,80,3,3))
test = np.array([[1,0,0],[0,1,0],[0,0,0]])
for i in xrange(a.shape[0]):
for j in xrange(b.shape[1]):
if (a[i,j] == test).all():
# Do something with indices i and j
该代码显然非常慢。我尝试使用numpy.where
,但它不起作用,因为它在四个指标中的每一个都寻求平等。
编辑:我也需要存储满足
哇,似乎工作。你认为它与'np.where(a == test).all(axis =(2,3)),1,0'完全相同吗?我发现这个工作,至少看起来像。 – fmonegaglia 2014-10-20 10:51:55
你的意思是'np.where(a == test).all(axis =(2,3))'?是的,这是相同的。看起来好多了。 – Phillip 2014-10-20 10:56:08