# Make a vector of sums of matrix rows

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... ;)

List@*Total /@ W

% === g

$\$ True

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}]
• Those are very helpful alternatives, thank's Sjoerd. Sorry I'm not allowed to accept both answers! – Sprog Jan 24 '16 at 19:57
• @sprog Luckily, you can vote for both. ;-) – Sjoerd C. de Vries Jan 24 '16 at 19:58
• @SjoerdC.deVries, I will do that :) – Algohi Jan 24 '16 at 19:59
• Aha! I have done! It will show when I have more reputation. – Sprog Jan 24 '16 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}}}];

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

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.

• +1 Although I am biased when it comes to using Total, I like linear algebra approaches. – Anton Antonov Jan 25 '16 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 '16 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 '16 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 '16 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 '16 at 16:20