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...
How can I do a cumulative sum?
-
- Member
- Posts: 42
- Joined: Wed Apr 06, 2011 8:20 am
-
- Super Administrator
- Posts: 4838
- Joined: Wed Mar 05, 2003 3:10 am
Re: How can I do a cumulative sum?
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 ) )
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
Re: How can I do a cumulative sum?
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.
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
-
- Member
- Posts: 42
- Joined: Wed Apr 06, 2011 8:20 am
Re: How can I do a cumulative sum?
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
subscript i ID = i
-
- Super Administrator
- Posts: 4838
- Joined: Wed Mar 05, 2003 3:10 am
Re: How can I do a cumulative sum?
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.
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.
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Units are important!
http://www.bbc.co.uk/news/magazine-27509559
Re: How can I do a cumulative sum?
Also, it could be wrapped in an INITIAL,
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.
Code: Select all
subscript i ID[i] = INITIAL(i)
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
-
- Member
- Posts: 42
- Joined: Wed Apr 06, 2011 8:20 am
Re: How can I do a cumulative sum?
Sorry if I already have definined my subscript i, I have to redefined? or create a new subscript?
ID is a new variable?
Thanks
ID is a new variable?
Thanks
Re: How can I do a cumulative sum?
ID is new. i is whatever dimension you already have.
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/