I have an numpy array that looks like this
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
Now I have an 2d array that tells the start index and the end index and the value to sum. like this
[[5, 12, 29] [8, 22, 719]]
I want to add these numbers to my initial array faster.
Currently my solution is
f= np.zeros(n+1, dtype = int)
# print(rounds)
for elem in rounds:
print('rounds : ',elem)
one = elem[0]
two = elem[1]
value_add = elem[2]
# for i in range(0,n+1):
for i in range(one-1,two):
f[i-1] += value_add
# print(f)
Its very slow when matrix gets too long, Any other way to optimize ?
2nd Iteration try for optimization
import numpy as np
f= np.zeros(n+1, dtype = int)
# print(rounds)
new = np.zeros(n+1)
for elem in rounds:
# prin t('rounds : ',elem)
one = elem[0]
two = elem[1]
value_add = elem[2]
# for i in range(0,n+1):
new[one-1:two] += value_add
[2, 5, 3]would result in the original changing to[1, 2, 5, 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]?