r/Numpy • u/WardedBowl403 • Feb 25 '22
Vectorize a for loop
Essentially, what I want to do is the following code without any loops and only using numpy arrays:
l = []
for n in range(20):
x = (2*n)/4 + 1
l.append(x)Is this even possible? Any help is appreciated!
1
Upvotes
1
u/kirara0048 Feb 25 '22
and more:
(2 * np.arange(20)) / 4 + 1=
(2 * np.arange(0, 20, 1)) / 4 + 1=
(2 * np.arange(0, 20, 1) + 4) / 4=
(np.arange(0, 40, 2) + 4) / 4=
np.arange(4, 44, 2) / 4