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 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}} 
share|improve this question
    
See also: mathematica.stackexchange.com/q/1805/5 –  rm -rf May 17 '12 at 19:40
add comment

2 Answers

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
share|improve this answer
3  
Aiming for brevity, you could also use: Tr /@ b –  Mr.Wizard May 17 '12 at 19:40
    
Precisely {#} & /@ Tr /@ b. –  Artes May 17 '12 at 22:22
1  
List is as short as {#}& :-) –  Mr.Wizard May 18 '12 at 3:07
add comment

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[Developer`ToPackedArray@b, {2}]}
share|improve this answer
    
I didn't realize that Plus can ever be faster than Total –  Szabolcs May 18 '12 at 7:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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