Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm simply looking for the {}+{} function for sublist elements of a list: (Parallel sums of elements)

c = {{1, 2, 3, 4}, {8, 7, 6, 5}};
Plus[c]
Mean[c]  
desired={9, 9, 9, 9}

Where Mean does directly what it's supposed to, unfortunately Plus doesn't. I tried Take and Part but they both do not seem to work here . Additionally, my c consists of a varying number of elements. I tried to work with Length[c] and Range .. ({}+{} is no solution as list element number varies) thank you for help!!

share

4 Answers 4

up vote 18 down vote accepted

Use either

Plus@@c

or

Total[c]
share
11  
For completeness: if one treats the list of lists as a matrix, Total[c] and Total[c, {1}] sums columns, Total[c, {2}] sums rows, and Total[c, 2] sums all elements. –  J. M. Feb 15 '12 at 23:29
3  
@J.M. Good points. For once, I did not try to be complete :) –  Leonid Shifrin Feb 15 '12 at 23:35
5  
Leonid, are you ill? :o) –  Mr.Wizard Feb 16 '12 at 5:10
    
@Mr.Wizard Looks like that :) –  Leonid Shifrin Feb 16 '12 at 13:36

Before version 5, I used Tr a lot for summing vectors as Total wasn't available:

c = {{1, 2, 3, 4}, {8, 7, 6, 5}};
Tr /@ Transpose[c]

(*
==> {9, 9, 9, 9}
*)
share
1  
+1 for didactic historical options –  Mr.Wizard Feb 16 '12 at 5:12

Use

Apply[Plus, {{1, 2, 3, 4}, {8, 7, 6, 5}}]
(*
==> {9, 9, 9, 9}
*) 

or its shortcut

Plus @@ {{1, 2, 3, 4}, {8, 7, 6, 5}}

Another way is

MapThread[Plus, {{1, 2, 3, 4}, {8, 7, 6, 5}}]
share

Here is another method that I hope will be instructive.

Sum[i, {i, c}]

{9, 9, 9, 9}

In TraditionalForm:

Mathematica graphics

share

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .