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?
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?
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.
Esc every time for Part brackets
– rm -rf
Oct 30 '12 at 14:27
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 *)
Last@Total[{{1, 3}, {2, 3}, {4, 1}}]
or
Total[{{1, 3}, {2, 3}, {4, 1}}]
for both components
{{1, 3}, {2, 3}, {4, 1}} // Query[Total, 2]
7
Unfortunately the current Query and associated Dataset implementation is riddled with workarounds - this is going to tech support >>
{{1, 3}, {2, 3}, {4, 1}} // Query[Total, 2] // Trace // LeafCount
3175
Anyone interested in a timing study?
{{1, 3}, {2, 3}, {4, 1}} // Query[Total, 2] // Trace // LeafCount results in 697 as of May 2023.
– Syed
May 12 '23 at 07:30
Partto get the elements from the list, thenTotalto sum them up. – b.gates.you.know.what Oct 30 '12 at 09:44Plus @@@ (m\[Transpose])will give you a list of the sums of the various elements of your list. – image_doctor Oct 30 '12 at 10:00Total[m[[All, 2]]]adds up the second column of m. Replace 2 with 1 to get the first column. Look upPart. – Cameron Murray Oct 30 '12 at 10:02Read the FAQs! 3) When you see good Q&A, vote them up byclicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem,by clicking the checkmark sign` – chris Oct 30 '12 at 10:26