【发布时间】:2016-01-06 22:48:32
【问题描述】:
假设我有两个用随机整数填充的二维数组。 我希望第一个数组保留所有值为 leq 5 的索引,第二个数组保留所有值为 leq 6 的索引。为简单起见,所有其他索引都可以设置为零。
table1 = np.array(([1,3],[5,7]))
table2 = np.array(([2,4],[6,8]))
print table1
print table2 ,'\n'
table1_tol_i = np.where(table1[:,:] > 5)
table1[table1_tol_i] = 0
print table1
table2_tol_i = np.where(table2[:,:] > 4)
table2[table2_tol_i] = 0
print table2, '\n'
[[1 3]
[5 7]]
[[2 4]
[6 8]]
[[1 3]
[5 0]]
[[2 4]
[0 0]]
然后我想只保留两个表都具有非零值的索引。我想要
[[1 3]
[0 0]]
[[2 4]
[0 0]]
我尝试了以下方法,但它没有给我想要的东西
nonzero_i = (np.where(table1 != 0) and (np.where(table2 != 0 )))
print table1[nonzero_i]
print table2[nonzero_i]
[1 3]
[2 4]
【问题讨论】:
-
您的描述准确地描述了我想要达到的目标,但不知道如何实现。