# 2D array summation operations

I am trying to figure out a way to solve my research problem. I have a 2D array, or so-called nested list with dimension let's say 3*30;

list = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.994429, 0.980929, 0.971825,
0.967241, 0.967241, 0.971825, 0.980929, 0.994429, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0.998888, 0.971825,
0.948683, 0.929755, 0.915302, 0.905539, 0.900617, 0.900617,
0.905539, 0.915302, 0.929755, 0.948683, 0.971825, 0.998888, 0, 0, 0,
0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0.971825, 0.939267, 0.910433,
0.885689, 0.865384, 0.849837, 0.839312, 0.834, 0.834, 0.839312,
0.849837, 0.865384, 0.885689, 0.910433, 0.939267, 0.971825, 0, 0, 0,
0, 0, 0, 0}}


So each row will have 30 elements and there are 3 rows totally.

The TableForm is clearly showing them:

Imagine this list has ten 3*3 element blocks. For example,the first and second block both has 9 zeros. I want to calculate the total value of each block and generate a new list in which every element is the summation for that small block. So it will generate 1*10 list in the end.

What I have done so far:

I think first I could partition the original list into some small groups.

list = Partition[Flatten[list], 3]


And then using Part to subtract the small groups and do further operations. Maybe something like list[[1]]+list[[11]]+list[[21]] could gives me similar results. But I am confused if I am doing this too clumsy. Could you give some of your thoughts? Appreciate the help!

## 3 Answers

BlockMap[Total[#, 2]&, list, {3, 3}]


{{0, 0, 2.90998, 6.5062, 8.1646, 8.1646, 6.5062, 2.90998, 0, 0}}

Also:

Partition[list, {3, 3}, {3, 3}, None, {}, Plus]


{{0, 0, 2.90998, 6.5062, 8.1646, 8.1646, 6.5062, 2.90998, 0, 0}}

• The first one is also very handy! Thanks for the sharing! Commented Jan 14, 2018 at 4:51
• @cj9435042, Thank you for the accept.
– kglr
Commented Jan 14, 2018 at 5:14

You can make a nested partition and sum at the two deepest levels

Total[Partition[list, {3, 3}], {3, 4}]

{{0, 0, 2.90998, 6.506198, 8.164602, 8.164602, 6.506198, 2.90998, 0, 0}}


Or more general:

block = {3, 3};
Total[Partition[list, block], {Length[block] + 1, ∞}]

• Actually,I like this one the most because it is very easy to understand. But I find that this may not apply to other dimensions , for example, for 5 * 5 blocks. Anyway a big thanks for the answer! Commented Jan 14, 2018 at 5:08

Because I wanted to use ArrayReshape(although it seems to be an overkill here):

array = Transpose /@ ArrayReshape[Transpose@list, {10, 3, 3}];
MatrixForm /@ array


Total /@ Flatten /@ array


{0, 0, 2.90998, 6.5062, 8.1646, 8.1646, 6.5062, 2.90998, 0, 0}

The Transpose /@ in array is not needed, I used it to obtain a clear display. In short:

Total /@ Flatten /@ ArrayReshape[Transpose@list, {10, 3, 3}]


gives the desired output.

• TakeList could be also used, but I don't have v11.2. Commented Jan 14, 2018 at 14:25