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

enter image description here

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

    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.

  • 1
    $\begingroup$ +1 Although I am biased when it comes to using Total, I like linear algebra approaches. $\endgroup$ – Anton Antonov Jan 25 '16 at 3:30
  • $\begingroup$ @AntonAntonov It is a shame that it is slightly slower than mapping Total. It is called Mathematica after-all. $\endgroup$ – Edmund Jan 25 '16 at 13:16
  • $\begingroup$ 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. $\endgroup$ – Anton Antonov Jan 26 '16 at 15:05
  • $\begingroup$ @AntonAntonov Humm. It seems that Total/@ is faster for small symbolic matrices but loses its appeal for large ones. See the update. Math wins. :-) $\endgroup$ – Edmund Jan 26 '16 at 16:12
  • 2
    $\begingroup$ @AntonAntonov Humm. Maybe not. If you swap the order in the With then Dot is noticeably slower and Total is noticeably faster. $\endgroup$ – Edmund Jan 26 '16 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.