I am doing a large data-set computation. Among those computational steps, in one step I need to do a sum with a pattern: Sum the elements with same interval.
For example, for a list from 1 to 9; With the interval being set to 3 manually. So it could be other values in different cases.
And the list would be calculated as:
1 + 4 + 7 = 12;
2 + 5 + 8 = 15;
3 + 6 + 9 = 18;
So for list = Range[1,9]
,the final desired result would be {12,15,18}
in this example. I attached an illustration for a further elaboration: sum the element with the same color when interval = 3:
Thanks for @Chris's Answer, the above case could be solved by:
Total[Partition[Range[9], 3]]
Edit my original question from here:
But what I actually want to do is only sum "N" numbers in a time. N is settled and when N = 3 in below example:
It should be computed by :
1 + 4 + 7 = 12;
2 + 5 + 8 = 15;
3 + 6 + 9 = 18;
10 + 13 + 16 = 39;
11 + 14 + 17 = 42;
12 + 15 + 18 = 45;
Hereby the result would be {12,15,18,39,42,45}
I think this might be not hard, but I just can't think it very clearly when I want to utilize the parallelization characteristics of MMA and trying to avoid Unpacked Array
results.
n
? If not, what is the desired output for inputsRange[19]
andRange[20]
? $\endgroup$ – kglr Dec 5 '18 at 3:09Mod[Length[list],N] = 0
, so no worry about corner cases $\endgroup$ – cj9435042 Dec 5 '18 at 3:13