summing over subscript ranges

Use this forum to post Vensim related questions.
Post Reply
chester riddalls
Junior Member
Posts: 14
Joined: Tue Mar 25, 2003 9:25 am

summing over subscript ranges

Post by chester riddalls »

Summing over the entire subscript range is easy using sum(!). My question is: Is there a way to sum over the first (or last) n elements in an m element range (m>n)?:(
Administrator
Super Administrator
Posts: 4609
Joined: Wed Mar 05, 2003 3:10 am

Post by Administrator »

You can use something like the following

sum variable[sub] =
sum (
if then else ( sub < m
, var[sub!]
, 0 )
)
chester riddalls
Junior Member
Posts: 14
Joined: Tue Mar 25, 2003 9:25 am

Post by chester riddalls »

thank you.
easy when you know how!
chester riddalls
Junior Member
Posts: 14
Joined: Tue Mar 25, 2003 9:25 am

Post by chester riddalls »

Have just actually tried this: it does not quite do it. It actually sums over all the subscripts for the first m-1 elements of sum variable, and puts zero in the rest. The sub without the exclamation mark is not summed over. Any thoughts?
chester riddalls
Junior Member
Posts: 14
Joined: Tue Mar 25, 2003 9:25 am

Post by chester riddalls »

Have figured it out. Need to define: sub element[sub] = sub. Then insert this (with an exclamation mark) instead of the sub on its own. Thanks
tomfid
Administrator
Posts: 3816
Joined: Wed May 24, 2006 4:54 am

Re: summing over subscript ranges

Post by tomfid »

If the number of elements you want to sum is fixed, it's more efficient to use a subrange, e.g.

sub : (s1-s10)
subr : (s1-s6)
myvar = SUM( myothervar[subr!] )

sums over the first 6 elements of the 10-element range, without having to iterate over the elements s7-s10 that aren't needed.

The VECTOR SELECT function is also often useful when you want to sum over sparse subsets of a range.

Tom
Post Reply