# Summation of values in an array

• 1st December 2015, 06:18 PM
ierofei
Summation of values in an array
Is there an expression to add up all of the values in a column or row of an array without typing out each position individually?

What I've been doing so far was:
Set Global Value A
>ValueAtXY ("Array", 1, 1) + ValueAtXY ("Array", 1, 2) + ValueAtXY ("Array", 1, 3)

.. And so on. I'm sure there's a shorter way to write that out, am I correct?
• 1st December 2015, 06:34 PM
schrodinger
Yeah, you should use loops

on "start sum" condition
(>>> set Global Value A to 0) if you want to reset the value each time you perform the calculation
>>> start loop "sum_array" DimY(array) times (since you seem to be summing Y's now, right?)

on loop "sum_array"
>>> Set Global Value A to Global Value A + ValueAtXY("Array", 1, loopindex("sum_array"))

if your array has 1 based index, use loopindex +1
• 1st December 2015, 06:53 PM
ierofei
Well that's pretty cool. To expand on the idea is there a way to exclude a specific position in the array from being included in this loop? E.g. ValueAtXY("Array", 1, 2)?

Oh and thanks!
• 1st December 2015, 07:11 PM
schrodinger
XD thanks for thanking

if you have to exclude only some specific positions
I would hard code them in the condition this way:

on loop "sum_array"
+loopindex("sum_array") <> number
>>> Set Global Value A to Global Value A + ValueAtXY("Array", 1, loopindex("sum_array"))
• 1st December 2015, 07:18 PM
ColdFire
It will be faster if he just use Add ValueAtXY("Array", 1, loopindex("sum_array")) to Global Value A instead of Set global value
• 1st December 2015, 08:49 PM
schrodinger
you're right, I copy pasted from first post and didn't think of that X)
• 1st December 2015, 08:59 PM
ierofei
Thank you both. I'll be able to build on this very easily!