15
$\begingroup$
list = {11.5575, 11.397, 5.52734, 4.0878, 2.54815, 1.86652, 2.55028,
2.14952, 1.6242, 1.34117}

I have a list of numbers. How do I make a function that creates a new list, where the first entry is equal to

{list[[1]], list[[1]] + list[[2]], list[[1]] + list [[2]] + list[[3]]} 

etc until the end of the list. I ask because my real list is quite a bit longer than just 10 entries, and writing this out would get far too long to handle.

$\endgroup$
3
  • 1
    $\begingroup$ This operation is known formally as the prefix sum. You may wish to investigate its parallel variants, an efficient implementation of which which could potentially outperform Accumulate. $\endgroup$ Commented Apr 21, 2013 at 5:33
  • 4
    $\begingroup$ For reference, for anyone searching, because it's what the title seems to ask, easy ways to sum a list (without accumulation of results) are Plus@@list and Total[list] $\endgroup$
    – user273
    Commented Apr 21, 2013 at 8:26
  • $\begingroup$ Closely related: (17759) $\endgroup$
    – Mr.Wizard
    Commented Feb 8, 2014 at 12:57

5 Answers 5

20
$\begingroup$

Accumulate is absolutely the most idiomatic and appropriate answer here. However since Mathematica is very powerful at list manipulation, it might be illustrative to show you couple of other ways of doing the same thing, just so you learn to think outside of mainstream procedural ways.

1. Using FoldList:

This is a functional way of doing exactly what you wrote by hand, and probably the first option you should be thinking of, if Accumulate were not available.

Rest@FoldList[Plus, 0, list]

The advantage of knowing this method, is that you can use it for other operations and not just addition.

2. Using ReplaceList

ReplaceList is very powerful when you want to apply the same operation (here Plus) to all possible results of a particular pattern (here, one that gives increasingly longer sublists starting with the first element).

ReplaceList[list, {h__, ___} :> Plus[h]]

Both the above approaches give you the same answer as Accumulate@list

$\endgroup$
0
27
$\begingroup$

Easy Solution

v = Accumulate[list]

Gives Output

{11.5575, 22.9545, 28.4819, 32.5697, 35.1178, 36.9843, 39.5346, \
41.6841, 43.3083, 44.6495}
$\endgroup$
8
$\begingroup$

Just for fun, here's a recursive approach:

accSum[{}] := {}; accSum[{x_}] := {x}; 
accSum[{r___, x1_}] := Join[{Total[{r, x1}]}, accSum[{r}]];

Usage:

list = {11.5575, 11.397, 5.52734, 4.0878, 2.54815, 1.86652, 2.55028,
    2.14952, 1.6242, 1.34117}

accSum[list] // Reverse

Gives:

{11.5575, 22.9545, 28.4818, 32.5696, 35.1178, 36.9843, 39.5346, 41.6841, 43.3083, 44.6495}

Updated

Here's an updated version of the above recursive function, it's shorter and there's no need to reverse the list obtained.

accSum2[{x___}] := {x};
accSum2[{r___, x_}] := Join[accSum2[{r}], {Total[{r, x}]}];

accSum2[list]

{11.5575, 22.9545, 28.4818, 32.5696, 35.1178, 36.9843, 39.5346, 41.6841, 43.3083, 44.6495}
$\endgroup$
3
$\begingroup$

Since this question has become an exposition of alternatives to Accumulate here is another:

list = {a, b, c, d, e};

x = 0;
Table[x += i, {i, list}]
{a, a + b, a + b + c, a + b + c + d, a + b + c + d + e}

The same method but with integrated scoping of x:

First @ Table[x += i, {x, {0}}, {i, list}]

(Module is more readable but since we're exploring alternatives...)

$\endgroup$
1
  • $\begingroup$ I also found this answer very useful as well. I will try it. $\endgroup$
    – omt66
    Commented Apr 26, 2019 at 7:00
0
$\begingroup$

This is a traditional way of doing it. Keeping it simple.

In[1]:= Total@list[[1 ;; #]] & /@ Range@Length@list

Out[1]= {11.5575, 22.9545, 28.4818, 32.5696, 35.1178, 36.9843, 39.5346, 41.6841, 43.3083, 44.6495}
$\endgroup$
1
  • 6
    $\begingroup$ This method has poor computational complexity because you total all values up to n every time, rather than using the result of the previous step. $\endgroup$
    – Mr.Wizard
    Commented Feb 8, 2014 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.