# Summing terms in Table

I have a table that looks like

Table[m, {k, 10}, {j, k}, {i, j}]


where, m is a square matrix that depends on the indices $i, j$, and $k$. I want to add all of the m matrices in the table. What is the most efficient way to do it?

If there are only two indices, I can do

Total[Apply[Join, Table[m, {k, 10}, {j, k}]]]


But this is not very fast.

I am trying to avoid nested sums, any suggestions are greatly appreciated!

• Plus@@(Table[m, {k, 10}, {j, k}, {i, j}])? – Algohi Apr 3 '15 at 22:23
• Flatten[result,2]//Total (result being the result of your table) should do what you're after... – ciao Apr 3 '15 at 22:26
• Thank you both. Algohi your method does not seem to work. rasher your method works, just to be clear if I have more indices I will need to Flatten at a higher level before the Total correct? Do you have any alternative approaches to this task? – user64620 Apr 3 '15 at 22:38
• Change Table to Sum. – 2012rcampion Apr 3 '15 at 22:49
• @user64620: Yes. I assumed you had the table and/or needed it as some intermediate result. If not, using Sum as commented by 2021rcampion is cleaner... – ciao Apr 4 '15 at 0:46

Total[Table[m, {k, 10}, {j, k}, {i, j}], -3]