Python Forum
Updating a matrix in a time interval inside a for loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Updating a matrix in a time interval inside a for loop (/thread-26886.html)



Updating a matrix in a time interval inside a for loop - vp1989 - May-17-2020

I would like to get a maximum of a matrix in intervals inside for loop.

Matrix K has dimension (N,3,3). I like to have a matrix P (Max of K) between N 1 to 10, and then update it again with K and find the maximum at N 10 to 20.

So far I can get the maximum of the matrix K but can't update it in each interval.

Thanks in advance.

L=3
N=30
Interval=10
VB=[]
for nstep in np.arange(N):
V=[]
for i in range(0,L):
U=[]
for j in range(0,L):
m=i+4*(i)*j*nstep-5*j*i
U.append(m)
V.append(U)
VB.append(V)
K=np.array(VB)
P=np.zeros((1,L,L))
for i in range(0,N):
for j in range(0,L):
for k in range(0,L):
if (K[i][j][k]>P[0][j][k]and(i%interval!=0)):
P[0][j][k]=K[i][j][k]



RE: Updating a matrix in a time interval inside a for loop - deanhystad - May-17-2020

Use python tags when posting code. I think I know what your program does, but without any indentation I cannot be sure.

If you want to keep P for each interval, why not do it this way:
L=3
N=30
Interval=10  # < Typo?  should be interval?
VB=[]
for nstep in np.arange(N):
    V=[]
    for i in range(0,L):
        U=[]
        for j in range(0,L):
            m=i+4*(i)*j*nstep-5*j*i
            U.append(m)
        V.append(U)
    VB.append(V)

at_intervals = []
K=np.array(VB)
P=np.zeros((1,L,L))
for i in range(0,N):
    for j in range(0,L):
        for k in range(0,L):
            if (K[i][j][k]>P[0][j][k]and(i%interval!=0)):
                P[0][j][k]=K[i][j][k]
                at_intervals.append(copy(P))



RE: Updating a matrix in a time interval inside a for loop - vp1989 - May-17-2020

Hi thanks for the reply, the interval is a typo.

I like to keep P for each interval. But a copy is not working in my code.

I should have P at 9,19, and 29 so that I can use it somewhere else.

Thanks again.


RE: Updating a matrix in a time interval inside a for loop - deanhystad - May-17-2020

You need to modify the logic. Right now you only save max (or make a copy) if interval coincides with K[ijk] > P[ijk]. You should split the two up.
            if (K[i][j][k]>P[0][j][k]:
                P[0][j][k]=K[i][j][k]
            if i % interval == 0:
                at_intervals.append(copy(P))
This will give you a copy at 0, 10, 20. If you want 9, 19, 29 the test is if (i+1) % interval == 0


RE: Updating a matrix in a time interval inside a for loop - vp1989 - May-17-2020

The logic worked thank you. :)