-2

I have two large matrices (1800L;1800C), epeq and triax, that have columns like:

epeq=
0
1
1
2
1
0
3
3
1
1
0
2
1
1
1

triax=
-1
1
3
1
-2
-3
-1
1
2
3
2
1
-1
-3
-1
1

as you can see, triax columns have cycles of positive and negative elements. I want a cumulative sum in epeq in the beginning of each cycle in triax and that this value stay constant during the cycle, like this:

epeq_cr=
0
1
1
1
1
1
1
11
11
11
11
11
11
11
11
17

and apply this procedure to all columns of the epeq matrix. I have that code but something miss.

epeq_cr = np.copy(epeq)
for g in range(1,len(epeq_cr)):
    for h in range(len(epeq_cr[g])):
        if (triax[g-1][h]<0 and triax[g][h]>0):
            epeq_cr[g][h] = np.cumsum()...
  • What are you summing up exactly? – Reut Sharabani Dec 16 '14 at 12:59
  • So you have points where a cycle starts (when triax crosses from negative to positive). The value in your output is the sum of all the values in epeq above the previous cycle start. – Aidan Kane Dec 16 '14 at 13:07
  • This is a curious question - I'll have a look to see if I can figure it out (it's probably something you could do with convolve in scipy but I'll just look at numpy). – Aidan Kane Dec 16 '14 at 13:08
  • I want sum all elements in epeq matrix above the beginning of each cycle in triax matrix and maintain this partial sum during the triax cycle. see the new epeq (last matrix in the example). I know that something miss in my np.cumsum but i don't know how to continue the code. – Tiago Azevedo Silva Dec 16 '14 at 13:09
  • That first value of 0 in the output array - Is that the sum of the 0 in the first row of the initial array, or is it sum of an empty list? – Aidan Kane Dec 16 '14 at 13:12
  • in all columns of epeq the first value is 0. My intention is start the loop in the second line of the epeq matrix because the first value is always 0. – Tiago Azevedo Silva Dec 16 '14 at 13:18
  • So, epeq and triax have slightly different shapes (triax is 1 longer than epeq) - is that correct? – Aidan Kane Dec 16 '14 at 13:22
  • No. triax matrix and original epeq matrix have the same shape. For that reason I compare the element in i line and the element in line i-1 of triax matrix to detect the beginning of each cycle triax and I start the loop in the second line. – Tiago Azevedo Silva Dec 16 '14 at 13:32

2 Answers2

0

I've run out of time to look at this now but I'd start by figuring out where the cycles start in the triax:

epeq = np.array([1, 1, 2, 1, 0, 3, 3, 1, 1, 0, 2, 1, 1, 1])
triax = np.array([-1, 1, 3, 1, -2, -3, -1, 1, 2, 3, 2, 1, -1, -3, -1, 1])

t_shift = np.roll(triax, 1)
t_shift[0] = 0
cycle_starts = np.argwhere((triax > 0) & (t_shift < 0)).flatten()
array([ 1,  7, 15])

So for any position, i, in epeq_cr you need to find the largest number less than i in cycle_starts and sum(epeq[:position]).

Aidan Kane
  • 3,856
  • 2
  • 25
  • 28
  • i'm a beginner in programming. I understand, you gave me a position of the beginning of each cycle. But how can I do this for all columns in epeq matrix? position is the cycle_starts? – Tiago Azevedo Silva Dec 16 '14 at 14:25
  • Ah ok. Well you're on the right track. This is a fairly complicated problem to tackle when you're just starting out! With numpy you can give it a 2D matrix (instead of 1D) and it will mostly work. You will have to change `t_shift[0] = 0` to something like `t_shift[0,:] = 0` to have it change the whole first row. – Aidan Kane Dec 16 '14 at 15:53
0
epeq_cr = np.copy(epeq)                                         
for g in range(1,len(epeq_cr)):
    for h in range(len(epeq_cr[g])):
        if (triax[g-1][h]<=0 and triax[g][h]>=0):
            epeq_cr[g][h]=sum(epeq[v][h] for v in range(g+1))
        else:
            epeq_cr[g][h]=epeq_cr[g-1][h]