Using Function in 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: Using Function in a For loop (/thread-26921.html) |
Using Function in a For loop - vp1989 - May-18-2020 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 RE: Using Function in a For loop - michael1789 - May-18-2020 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. RE: Using Function in a For loop - vp1989 - May-18-2020 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 RE: Using Function in a For loop - pyzyx3qwerty - May-19-2020 So what is your output? And what are your errors (if any) ? RE: Using Function in a For loop - jefsummers - May-19-2020 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. RE: Using Function in a For loop - vp1989 - May-19-2020 (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. 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. |