-2

I have an array of three dimension

x[i,j,k]=[[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]], [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]], [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]], [[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]]

And I need cumulative sum like the following

y[i][j][k]=[[[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]], [1, 21], [3, 28], [6, 36], [10, 45], [15, 55]], [1, 21], [3, 28], [6, 36], [10, 45], [15, 55]], [1, 21], [3, 28], [6, 36], [10, 45], [15, 55]]]]

I have tried

for k in range(0,1):
    for j in range(0,5):
        for i in range(0,4):
            y[i][j][k]=sum(sum(x[i][j][k] for jj in range(0,5) if jj<=j)for kk in range(0,1) if kk<=k)

but I got

y[i][j][k]=[[[1, 12], [3, 26], [6, 42], [10, 60], [15, 80]], [[1, 12], [3, 26], [6, 42], [10, 60], [15, 80]], [[1, 12], [3, 26], [6, 42], [10, 60], [15, 80]], [[1, 12], [3, 26], [6, 42], [10, 60], [15, 80]]]

How to do for loop as per my need?

I have

x[0][0][0]=1
x[0][1][0]=2
x[0][2][0]=3
x[0][3][0]=4
x[0][4][0]=5
x[0][0][1]=6
x[0][1][1]=7
x[0][2][1]=8
x[0][3][1]=9
x[0][4][1]=10

I need to do

y[0][0][0]=x[0][0][0]=1
y[0][1][0]=x[0][0][0]+x[0][1][0]=3
y[0][2][0]=x[0][0][0]+x[0][1][0]+x[0][2][0]=6
y[0][3][0]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]=10
y[0][4][0]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]=15


y[0][0][1]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]+x[0][0][1]=21
y[0][1][1]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]+x[0][0][1]+x[0][1][1]=28
y[0][2][1]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]+x[0][0][1]+x[0][1][1]+x[0][2][1]=36
y[0][3][1]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]+x[0][0][1]+x[0][1][1]+x[0][2][1]+x[0][3][1]=45
y[0][4][1]=x[0][0][0]+x[0][1][0]+x[0][2][0]+x[0][3][0]+x[0][4][0]+x[0][0][1]+x[0][1][1]+x[0][2][1]+x[0][3][1]+x[0][4][1]=55
Deepan
  • 9
  • 4
  • 1
    Shouldn't it be `[[1, 6], [3, 13], [6, 21], [10, 30], [15, 40]]` for each sublist? Where does the `21` in your expected `[1, 21]` come from? – blhsing Aug 27 '21 at 08:41
  • 21 is (1+2+3+4+5+6) that cumulative of one dimension carry forward to next dimension. (That is cumulative of cumulative array) – Deepan Aug 27 '21 at 08:43
  • What code did you produce to achieve what you want to be done? [mre]? what is your codes problem? – Patrick Artner Aug 27 '21 at 08:46
  • What would the desired result look like? – martineau Aug 27 '21 at 09:01
  • @blhsing The numbers 2,3,4,5 are in the before dimensions. I edited my requirements clearly now as you can check above. – Deepan Aug 27 '21 at 09:02

1 Answers1

0

You can do the following, using some transpositioning trickery:

from itertools import accumulate, chain

result = []
for l in x:
    a = [*accumulate(chain(*zip(*l)))]  # [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
    result.append([*map(list, zip(a[:len(l)], a[len(l):]))])

[[[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]],
 [[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]],
 [[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]],
 [[1, 21], [3, 28], [6, 36], [10, 45], [15, 55]]]

    
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • Thank you, But I need to do it within the loop as I have some related operations and functions dependent on this value. Is there any way to do cumsum within the loop itself – Deepan Aug 27 '21 at 08:53