Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list of rows in database such as

{{a,b,c}, {d,e,f},{g,h,i}}

I want to be able to add each row across and each column down (like a spreadsheet). In other words be able to pick columns and rows and add down or across.
Could you point me in the right direction?

share|improve this question

3 Answers

Use Total with the appropriate second argument to sum the matrix along rows/columns.

Sum along rows:

m = {{a,b,c}, {d,e,f},{g,h,i}};
Total[m, {1}]
(* {a + d + g, b + e + h, c + f + i} *)

By default, Total[m] (without a second argument) sums along the rows.

Sum along columns

Total[m, {2}]
(* {a + b + c, d + e + f, g + h + i} *)
share|improve this answer
I will never know what "along columns" and "along rows" mean. I would have guessed the opposite. – Rojo Jan 27 at 0:46
Thanks! The hard part of Mathematica is knowing the commands! – David Kerr Jan 27 at 0:47
In the second case, a true mathematician would've transposed the matrix and exultantly said ... – belisarius Jan 27 at 6:39
I think you may have confused rows and columns. By default Total sum along the columns. – Mr Alpha Jan 27 at 12:19
@MrAlpha I guess it depends on what one means by "along the columns". As with Rojo, I can never seem to remember what the right/current/popular interpretation is, but since this is binary, just flip it accordingly :) – rm -rf Jan 27 at 14:42

You could get both the row and column sums at once with a simple function:

rowColSum[m_?MatrixQ] := {Plus @@@ m, Plus @@@ Transpose@m}

m = ArrayReshape[Range@6, {2, 3}]

{{1, 2, 3}, {4, 5, 6}}

rowColSum@m

{{6, 15}, {5, 7, 9}}

If you were interested in getting spreadsheet-like output, you could do it this way:

tabulate[m_?MatrixQ] := Module[{rs, cs},
  rs = Plus @@@ m;
  cs = Append[Plus @@@ Transpose@m, ""];
  Append[MapThread[Append, {m, rs}], cs]]

tabulate@m // TableForm

enter image description here

Update

I would like to satisfy Mr.Wizard's request for color, but his specifications were rather vague. I hope the following will satisfy him.

colorPattern = (_RGBColor | _GrayLevel | _Hue);

wizardStyleTabulate[m_?MatrixQ,
                    dataColor : colorPattern : Black, 
                    sumColor : colorPattern : Blue] :=
Module[{data, rs, cs},
  data = Map[Style[#, dataColor] &, m, {-1}];
  rs = Style[#, sumColor] & /@ Plus @@@ m;
  cs = Style[#, sumColor] & /@ Append[Plus @@@ Transpose@m, ""];
  Append[MapThread[Append, {data, rs}], cs]]    

m // wizardStyleTabulate // TableForm

default-styled-image

wizardStyleTabulate[m, Red, Hue[0.55]] // TableForm

styled-image

share|improve this answer
Add colors to the output (so that the numerals are not all black) and you'll get my vote. – Mr.Wizard Jan 27 at 3:39
Absolutely satisfied. Belated +1! – Mr.Wizard Jan 31 at 8:29
 m = {{a, b, c}, {d, e, f}, {g, h, i}};

Update: Tr

Tr /@ (m\[Transpose])       (* column sums *)
Tr[m, Plus, 1]              (* column sums *)
Tr/@m                       (* row sums    *)
Tr[m\[Transpose], Plus, 1]  (* row sums    *)

Column sums

Total@m 
Plus @@ m 
Fold[Plus, First@m, Rest@m]
ConstantArray[1, 3].m 
Flatten@ListConvolve[{ConstantArray[1, 3]}, Transpose@m]
(* {a+d+g, b+e+h, c+f+i} *)

Row sums

Total /@ m
Plus @@@ m
Fold[Plus, First@#, Rest@#] &[Transpose@m]
m.ConstantArray[1, 3]
Flatten@ListConvolve[{ConstantArray[1, 3]}, m]
(* {a+b+c, d+e+f, g+h+i} *)
share|improve this answer
Thanks too, very helpful! – David Kerr Jan 27 at 0:48
1  
How do you pick only one column or row? – David Kerr Jan 27 at 0:58
1  
@DavidKerr, Total@m[[2]] (* sum of row 2 *), Total@m[[All, 2]] (* sum of column 2 *), and Total@m[[All, {2, 3}]] (* sum of column 2 and sum of column 3*) ... – kguler Jan 27 at 1:09
+1 for method overkill – Mr.Wizard Jan 27 at 3:37

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.