0

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()

Ali_Sh
  • 2,667
  • 3
  • 43
  • 66
phryas
  • 19
  • 1
  • try : `np.where((bz_2D_surface3 >0) & (jz_2D_surface3 >0))` – I'mahdi Jun 28 '22 at 05:42
  • this will help you: https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous – Dev Jun 28 '22 at 06:46

1 Answers1

0

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]
Ali_Sh
  • 2,667
  • 3
  • 43
  • 66