Reputation: 19
I have two 2D arrays in NumPy of equal size, and am trying to identify the indices where two conditions are met. Here's what I try and what I get. Any suggestions? I'm using np.where
and this does not seem to be the correct choice.
Thanks for any help.
ind_direct_pos = np.where((bz_2D_surface3 > 0.0) and (jz_2D_surface3 > 0.0))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Upvotes: 0
Views: 89
Reputation: 2816
If the two arrays with the same size, not the same shape, we can ravel
it at first (using and
instead &
is a cause of this problem as I'mahdi comment):
bz_2D_surface3 = np.array([[1, 3], [4, -1], [-3, -6], [-1, 2], [1, 3], [2, -3]])
jz_2D_surface3 = np.array([[-1, 3, 3], [-4, -1, 2], [3, -1, -5], [1, 2, 3]])
# pair_wise
np.transpose(np.where((bz_2D_surface3.ravel() > 0) & (jz_2D_surface3.ravel() > 0)))
# [[ 1]
# [ 2]
# [ 9]
# [10]]
if we have two arrays with the same shape:
bz_2D_surface3 = np.array([[1, 3], [4, -1], [-3, -6], [-1, 2], [1, 3]])
jz_2D_surface3 = np.array([[-1, 3], [-4, -1], [3, -1], [1, 2], [1, 11]])
# pair_wise
np.transpose(np.where((bz_2D_surface3 > 0) & (jz_2D_surface3 > 0)))
# [[0 1]
# [3 1]
# [4 0]
# [4 1]]
# row_wise
np.where(((bz_2D_surface3 > 0.0).all(1)) & ((jz_2D_surface3 > 0.0).all(1)))[0]
# [4]
Upvotes: 0