Python Forum
Updating a matrix in a time interval inside a for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Updating a matrix in a time interval inside a for loop
#1
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]
Reply
#2
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))
Reply
#3
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.
Reply
#4
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
Reply
#5
The logic worked thank you. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 375 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,526 Nov-07-2023, 09:49 AM
Last Post: buran
  Check if two matrix are equal and of not add the matrix to the list quest 3 778 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Mapping a value to an interval JazonKuer 12 1,841 Mar-17-2023, 07:59 PM
Last Post: Gribouillis
  Help adding a loop inside a loop Extra 31 4,336 Oct-23-2022, 12:16 AM
Last Post: Extra
  Confidence interval after doing penalised cox regression HatemAli 0 1,111 Feb-23-2022, 11:02 PM
Last Post: HatemAli
  How to immediately kill and restart a thread while using a time.sleep() inside it? philipbergwerf 4 3,452 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,303 May-03-2021, 06:30 AM
Last Post: Gribouillis
  How to measure execution time of a multithread loop spacedog 2 2,835 Apr-24-2021, 07:52 AM
Last Post: spacedog
  Adding to the dictionary inside the for-loop - weird behaviour InputOutput007 5 2,651 Jan-21-2021, 02:21 PM
Last Post: InputOutput007

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020