Reputation: 530
If I have an array of time increments, for example:
intervals = np.random.normal(loc=1,scale=0.1,size=100)
one possible way to create the corresponding array of time instants is to create a list and manually make the sum:
Sum=0.
instants=[]
for k in range(len(intervals)):
Sum+=intervals[k]
instants.append(Sum)
instants=np.array(instants)
So, I have just switched from a array of dt(i)
to an array of t(i)
.
But usually python offers elegant alternatives to using for
loops. Is there a better way to do it?
Upvotes: 1
Views: 41
Reputation: 476557
What you here describe is the cumulative sum. You can calculate this with .cumsum()
[numpy-doc]:
intervals.cumsum()
For example:
>>> intervals
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> intervals.cumsum()
array([ 0, 1, 3, 6, 10, 15, 21, 28, 36, 45])
Upvotes: 1