Home > OS >  ValueError when applying conditions on two different arrays in numpy
ValueError when applying conditions on two different arrays in numpy

Time:10-08

I want to populate an array depending on two conditions depending on the contents of two different arrays respectively:

c = np.where(a == b, 0, 99)
c = np.where(a == 2 and b == 1, -1, c)

Arrays a, b and c all have the same shape. In case the element of array a=2 AND the element of the same index of array b=1, I want to update the element of that index in array c with -1. Otherwise, just keep the previous value of array c.

However, I get this error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Where am I wrong? Also tried this with no success:

c = np.where(a == b, 0, 99)
c = np.where(np.all(a == 2 and b == 1), -1, c)

CodePudding user response:

 (a == 2) & (b == 1)

is the correct syntax

  • Related