Page 1 of 1

How can I do a cumulative sum?

Posted: Wed Mar 13, 2013 4:44 pm
by luisangelguzmang
I need to do a cumulative sum over a vector according to V=ai+ai-1. For example:
Vector = [2, 4, 6, 3, 8, 1]
I need a new vector: V*=[2, 6, 12, 15, 23, 24]

There are a function for this?
Thks...

Re: How can I do a cumulative sum?

Posted: Wed Mar 13, 2013 7:00 pm
by Administrator
The following should work (sorry, I don't have time to test it right now).

Code: Select all

subscript i ID = i

v*[i] = sum ( if then else ( subscript i ID[i!] <= i , vector[i!] , 0 ) )

Re: How can I do a cumulative sum?

Posted: Wed Mar 13, 2013 7:03 pm
by tomfid
See the sample model in http://www.ventanasystems.co.uk/forum/v ... f=2&t=5202 for an example of an efficient approach to iteration/cumulation with ss mapping.

Re: How can I do a cumulative sum?

Posted: Wed Mar 13, 2013 9:33 pm
by luisangelguzmang
Thks, but I do not quite understand the code. I already got the subindex i, I don“t understand this part:

subscript i ID = i

Re: How can I do a cumulative sum?

Posted: Wed Mar 13, 2013 11:14 pm
by Administrator
Sorry, I should have written
subscript i ID = i

This is a new parameter. It is basically the value of i so that it can be used in the sum calculation.

Re: How can I do a cumulative sum?

Posted: Thu Mar 14, 2013 2:19 pm
by tomfid
Also, it could be wrapped in an INITIAL,

Code: Select all

subscript i ID[i] = INITIAL(i)
The mapping approach in the sample model linked is more efficient, O(i) time vs. O(i^2) here, but more work to set up, so if your model is small, this approach is easier.

Re: How can I do a cumulative sum?

Posted: Thu Mar 14, 2013 5:07 pm
by luisangelguzmang
Sorry if I already have definined my subscript i, I have to redefined? or create a new subscript?
ID is a new variable?
Thanks

Re: How can I do a cumulative sum?

Posted: Thu Mar 14, 2013 10:34 pm
by tomfid
ID is new. i is whatever dimension you already have.