Python Forum
Using Function in a For loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Function in a For loop
#1
Using function variable C from the previous iteration and then calculate VB for the current iteration. Then Use VB to calculate C from the next iteration.
I am facing the problem to sync the loop and updating VB and C in each iteration. I have tried some coding.
Thanks in advance.
L=9
N=10
M=5
VB=[]
C=[]
K=[]
C=[]
P=np.zeros((1,L,L))
omega=[]
def g():     ## A function to calculate C
    for i in range(0,M):  
        if(i%M==0):   ###### getting a max in a interval 
            for j in range(0,L):
                for k in range(0,L):
                    P[0][j][k]=K[i][j][k]     ##### the K is comeing from the main body 
        elif(i%M!=0):
            for j in range(0,L):
                for k in range(0,L):
                    if (K[i][j][k]>P[0][j][k]): ##### getting max value 
                        P[0][j][k]=K[i][j][k]
        if((i+1)%M==0):   #### saving max value for the current interval 
            C=P.copy()
        return C
###### get C for the interation 

for NJ in (0,10):
    for nstep in np.arange(N):
        V=[]
        for i in range(0,L):
            U=[]
            for j in range(0,L):
                m=18+5*i*nstep/2-6*j*i*nstep
                U.append(m)
            V.append(U)
        VB.append(V)### calculate VB for the current iteration
        C=g() ### calling C from the last iteration 
    K[NJ]=np.array(VB)  ### Use C from the last iteration and mulitying in the current iteration
# use this K again usded in function g return C for the next iteration  
Reply
#2
It's like reading a bowl of alphabet soup.

Using single letters as variable names makes it impossible for someone to understand at a glance. Try using actual/descriptive names for things. It will help you if you have to look back at code later or if you have to share it.
Reply
#3
I simplified the code for the readers.
import numpy as np
import sys
import math
import copy
row_matrix=9   #row
coulomn_matrix=9
tstep=10 #number of one iteration  
interval=5 #lenghth of one interval
max_matrix=np.zeros((1,row_matrix,coulomn_matrix)) #maximum of matrix
maxm=[] # used for copy max_matrix
mat=[]
matrix=[]
save_matrix=[]
for N in range(0,5):
   if (N==0):   # maxm initiation
       maxm=zeros(row_matrix,Dim)
   else: 
       def max():     ## A function to calculate C
        for i in range(0,interval):  
            if(i%interval==0):   ###### getting a max in a interval 
                for j in range(0,row_matrix):
                    for k in range(0,coulomn_matrix):
                        max_matrix[0][j][k]=mat[i][j][k]     ##### the K is comeing from the main body 
            elif(i%interval!=0):
                for j in range(0,row_matrix):
                    for k in range(0,coulomn_matrix):
                        if (mat[i][j][k]>max_matrix[0][j][k]): ##### getting max value 
                            max_matrix[0][j][k]=mat[i][j][k]
            if((i+1)%interval==0):   #### saving max value for the current interval 
                maxm=P.copy()
            return maxm
###### get maxm for each interval 
    for nstep in np.arange(tstep):
        V=[]
        for i in range(0,row_matrix):
            U=[]
            for j in range(0,row_matrix):
                m=18+5*i*nstep/2-6*j*i*nstep*N*N  #calculation of value of indicies
                U.append(m)                       
            V.append(U)
        matrix.append(V)### getting the matrix 
    mat=np.array(matrix)   ### getting arrays to use in functioin max 
save_matrix.append(matrix) ###saving matrix at each interval               
Reply
#4
So what is your output? And what are your errors (if any) ?
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#5
One issue is indentation. Line 43 is at the same level as the for loop. Don't think that is intentional, though might be depending on your logic. Bigger issue - in the else clause you define a function. Bad idea. Means the function gets defined each time that else clause is executed. Not only that, it gets defined but not called.

Move your function definition out of the loop. Then call it in your else clause. And check the indentation at the end of the program to be sure it does what you think.
Reply
#6
(May-19-2020, 11:52 AM)jefsummers Wrote: One issue is indentation. Line 43 is at the same level as the for loop. Don't think that is intentional, though might be depending on your logic. Bigger issue - in the else clause you define a function. Bad idea. Means the function gets defined each time that else clause is executed. Not only that, it gets defined but not called.

Move your function definition out of the loop. Then call it in your else clause. And check the indentation at the end of the program to be sure it does what you think.

I was facing problem for defining Function outside the loop, therefore I put it inside the function.
Even without line 43, the code in not working properly.

I need to store mat for every N.

Sorry I am new in python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Exit Function - loop Tetsuo30 2 2,065 Sep-17-2020, 09:58 AM
Last Post: Tetsuo30

Forum Jump:

User Panel Messages

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