I am trying to work with a combination of arrays and if statements. I am checking if two elements in two different 1-D arrays return True. (I've gotten that far) Then I am trying to pass that True / False into an if statement - but for every result in the array (not all, or any). I'm not sure how to structure the if statements I'm trying to create.
The below code is what I'm to pass into the if statement. When printed alone, it works as I believe it should, returning a boolean array.
l0, l1, l2, l3 are arrays of numbers, I've checked their outputs individually
test = np.array(np.greater_equal(l0,l1))
print(test)
[[False]
[False]
[False]
[False]
[False]
[False]
[False]
[False]
[False]
[False]
[False]
[ True]
[ True]
[ True]
[ True]
[ True]
. . . . continues
Seen below, when I try to use the output of the array in a normal If statement, I encounter an error citing ambiguity. I am not trying to return true if "all" or "any" of the elements in the array are >= each other, I want to go through one at a time and assign the variables as written below in either case.
test = np.array(np.greater_equal(l0,l1))
if test:
cu1 = l0 - l1
cd1 = 0
else:
cd1 = l1 - l0
cu1 = 0
Error: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Any ideas on proper construction in a way I can pass the indexed position into the if statement and perform further calculations with the resulting variable assignments? (below)
full context of what I'm working with if it helps
if np.array(np.greater_equal(l0,l1)):
cu1 = l0 - l1
cd1 = 0
else:
cd1 = l1 - l0
cu1 = 0
if np.array(np.greater_equal(l1,l2)):
cu2 = cu1 + l1 - l2
cd2 = cd1
else:
cd2 = cd1 + l2 - l1
cu2 = cu1
if np.array(np.greater_equal(l2,l3)):
cu = cu2 + l2 - l3
cd = cd2
else:
cu = cu2
cd = cd2 + l3 - l2
if np.array(np.not_equal(np.sum(cu + cd),0)):
x = cu / (cu + cd)
else:
x = 0
x = np.array(x)
forloops are just as basic constructs asif/elseones.ifis not a iterator, it's a simple true/false switch.