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.

How can I add the elements in the sublists?
For example, if I have the list which is

 m={{1,3},{2,3},{4,1}}

then, the output that I want is 3+3+1=10. How can I do this?

share|improve this question
    
You can use Part to get the elements from the list, then Total to sum them up. –  b.gatessucks Oct 30 '12 at 9:44
10  
$3+3+1\neq 10\phantom{}$ –  J. M. Oct 30 '12 at 9:57
1  
Plus @@@ (m\[Transpose]) will give you a list of the sums of the various elements of your list. –  image_doctor Oct 30 '12 at 10:00
3  
@J.M. which is why this is a tricky question ;-) –  chris Oct 30 '12 at 10:25
3  
@J.M. base 7? :^) –  Mr.Wizard Oct 30 '12 at 10:56

3 Answers 3

I thinks the cleanest way is:

Total[m[[All, 2]]]

using Esc[[Esc and Esc]]Esc.

If you use it a lot (like me), you can create shortcuts as explained here.

share|improve this answer
    
You can use these shortcuts if you don't like pressing Esc every time for Part brackets –  rm -rf Oct 30 '12 at 14:27
    
Yes, I use it. I put in the post. Tks –  Murta Oct 30 '12 at 18:46

Following @b.gatessucks

 Total[m[[;; , 2]]]

  (* 7 *)

If you want to sum all components (as @image_doctor pointed out too, sorry I missed that)

 Plus @@@ Transpose[m]

 (* 7 7 *)
share|improve this answer
1  
Total[m[[All , 2]]] works as well. :) –  J. M. Oct 30 '12 at 12:05
Last@Total[{{1, 3}, {2, 3}, {4, 1}}]

or

Total[{{1, 3}, {2, 3}, {4, 1}}]

for both components

share|improve this answer

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.