I am wondering the possibility of turning a for loop with several if-else statements into vectorize version in Python. Suppose I have several numpy arrays a,b,c,d
, where a,b,c,d
have the same length. I need to write the following for loop to calculate the value of c
and ‘d’
for i in range(2, len(a)):
if (
a[i - 2] < b[i - 2]
and a[i - 1] > b[i - 1]
and c[i - 1] == 0
):
d[i] = -1
c[i] = c[i - 1] - 1
elif (
a[i - 2] > b[i - 2]
and a[i - 1] < b[i - 1]
and c[i - 1] == 0
):
d[i] = 1
c[i] = c[i - 1] + 1
else:
d[i] = 0
c[i] = c[i - 1]
The catch is that the size of arrays is 10^5
and I have to execute this for-loop for 10^3' times, the time complexity is
O(10^8)` which is very slow and computational heavy to use for loop with if-else statement. I am wondering is there a vectorized or faster way to do these operation in Python?
I have ask ChatGPT for helps, it suggests that turning the if
, elif
conditions to
condition1 = (
(a[:-2] < b[:-2]) &
(a[1:-1] > b[1:-1]) &
(d 1:-1] == 0)
)
condition2 = (
(a[:-2] > b[:-2]) &
(a[1:-1] < b[1:-1]) &
(d 1:-1] == 0)
)
, respectively. Then, it uses np.where
to execute the conditions
c[2:] = np.where(condition1, -1,0)
d[2:] = np.where(condition1, d[1:-1] - 1, d[1:-1])
c[2:] = np.where(condition2, 1, 0)
d[2:] = np.where(condition2, d1:-1] + 1, d[1:-1])
however, these results are not correct comparing the for-loop result. Beside, I don’t know where is the condition for else
statement.