4
$\begingroup$

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:

enter image description here

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!

$\endgroup$
5
$\begingroup$
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}}

$\endgroup$
  • $\begingroup$ The first one is also very handy! Thanks for the sharing! $\endgroup$ – cj9435042 Jan 14 '18 at 4:51
  • $\begingroup$ @cj9435042, Thank you for the accept. $\endgroup$ – kglr Jan 14 '18 at 5:14
4
$\begingroup$

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, ∞}]
$\endgroup$
  • $\begingroup$ 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! $\endgroup$ – cj9435042 Jan 14 '18 at 5:08
2
$\begingroup$

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

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

enter image description here

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.

$\endgroup$
  • $\begingroup$ TakeList could be also used, but I don't have v11.2. $\endgroup$ – corey979 Jan 14 '18 at 14:25

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.