# Add elements in a sublist

I have created a subset of arrays:

b = Subsets[Range[10], {5}]
(* Out= {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 6}, {1, 2, 3, 4, 7}, {1, 2, 3, 4, 8},
{1, 2, 3, 4, 9}, ..., {6, 7, 8, 9, 10}} *)


This command shows the combinations I wish to evaluate with. How can I add the elements of each subset in the list above to get an output like:

{{15}, {16}, {17}, {18}, {19},..., {40}}

• – rm -rf
Commented May 17, 2012 at 19:40

Not sure I follow, but maybe Total[b, {2}]?

If the list of lists form is important: Transpose@{Total[b, {2}]}, or more simply as Artes just posted: List /@ Total[b, {2}]

Subsets returns an unpacked array, and in that case Plus @@@ b is faster. However, by packing the array this method will be an order of magnitude faster than Plus:

Transpose@{Total[DeveloperToPackedArray@b, {2}]}

• I didn't realize that Plus can ever be faster than Total Commented May 18, 2012 at 7:56

Use Applyon the first level of the list b (@@@ is a shorthand), i.e.

Plus @@@ b


Or exactly what you want, then map List over Plus @@@ b, i.e.

List /@ Plus @@@ b

• Aiming for brevity, you could also use: Tr /@ b Commented May 17, 2012 at 19:40
• Precisely {#} & /@ Tr /@ b. Commented May 17, 2012 at 22:22
• List is as short as {#}&` :-) Commented May 18, 2012 at 3:07