Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a matrix in Mathematica:

W = {{Subscript[x, 11], Subscript[x, 12], Subscript[x, 13]},
    {Subscript[x, 21], Subscript[x, 22], Subscript[x, 23]},
    {Subscript[x, 31], Subscript[x, 32], Subscript[x, 33]}}

I would like to make a column vector (called g), where each row (i.e. each element) is the sum of the equivalent row in the matrix W.

My code for g is, at the moment:

g = {{Total[W[[1]]]}, {Total[W[[2]]]}, {Total[W[[3]]]}}

This is fine for this situation, where the matrix W only has a few rows. But if matrix W were giant, this wouldn't be a great approach, as I would need to index every single row in the matrix when defining g.

I'm new to Mathematica, so I thought that the answer might involve the Do function, for a matrix with a total of A rows...

Do[Subscript[g, i] = Total[W[[i]],{1,A}]

...but this doesn't seem to solve the issue.

I would be very grateful if anyone could point me in the right direction. I am sure this is a very simple question for all you Mathematica whizzes... ;)

share|improve this question
up vote 7 down vote accepted
List@*Total /@ W

enter image description here

% === g

$\ $ True

share|improve this answer
List@*Total /@ W (V10 only)
List /@ Total /@ W (V10 or earlier)
(* {{Subscript[x, 11] + Subscript[x, 12] + Subscript[x, 13]}, 
    {Subscript[x, 21] + Subscript[x, 22] + Subscript[x, 23]}, 
    {Subscript[x, 31] + Subscript[x, 32] + Subscript[x, 33]}} *)

other alternatives:

List /@ Plus @@@ W
List /@ Total[W, {2}]
share|improve this answer
Those are very helpful alternatives, thank's Sjoerd. Sorry I'm not allowed to accept both answers! – Sprog Jan 24 at 19:57
@sprog Luckily, you can vote for both. ;-) – Sjoerd C. de Vries Jan 24 at 19:58
@SjoerdC.deVries, I will do that :) – Algohi Jan 24 at 19:59
Aha! I have done! It will show when I have more reputation. – Sprog Jan 24 at 19:59

Algebra approach. Dot Wwith a column vector of 1's.

W . ConstantArray[1, {Last@Dimensions@W, 1}]

Was just curious if Dot approach was faster than Total/@ for large symbolic matrices after reading comments.

sqSymMx[m_Symbol, n_Integer?Positive] := 
  Table[Indexed[m, {i, j}], {i, n}, {j, n}];

t = With[{r = sqSymMx[x, #]},
      First /@ {AbsoluteTiming[Total /@ r;], 
        AbsoluteTiming[r.ConstantArray[1, {Last@Dimensions@r, 1}];]}
      }] & /@ Range[100, 2000, 100];

opts = Sequence[PlotStyle -> {Gray, LightGray}, 
   PlotLegends -> {Total, Dot}, ImageSize -> Medium, 
   Filling -> {1 -> {{2}, {Red, Green}}}];

    ListPlot[#, opts] &,
    ListLogPlot[#, opts] &
    }[Inner[List, t[[All, 1]], #, List] & /@ Transpose@t[[All, 2]]]]

enter image description here

It appears it is even though Total/@ is faster for small symbolic matrices. This is interesting considering that the Dot method also needs to also build its column vector of ones.

Well, maybe not. If you swap the order the With then Dot is noticeably slower and Total is noticeably faster. Some caching of the execution or something I imagine. My kernel savvy is practically non-existent.

share|improve this answer
+1 Although I am biased when it comes to using Total, I like linear algebra approaches. – Anton Antonov Jan 25 at 3:30
@AntonAntonov It is a shame that it is slightly slower than mapping Total. It is called Mathematica after-all. – Edmund Jan 25 at 13:16
Total has some very advanced mathematical algorithms in it. (It is not just Plus@@#&.) Also, Total is expected to be faster than the corresponding matrix operations both sparse and dense. Nevertheless, it would be interesting to compare the timings of Total vs. Dot, especially for sparse arrays. – Anton Antonov Jan 26 at 15:05
@AntonAntonov Humm. It seems that Total/@ is faster for small symbolic matrices but loses its appeal for large ones. See the update. Math wins. :-) – Edmund Jan 26 at 16:12
@AntonAntonov Humm. Maybe not. If you swap the order in the With then Dot is noticeably slower and Total is noticeably faster. – Edmund Jan 26 at 16:20

Your Answer


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.