user4478336
user4478336

Reputation:

Indexerror: list index out of range/numpy

I am really new to python. I am getiing an error stating Indexerror list index out of range. Kindly help me out. Thanks in advance . Edit 1

x = np.array([10,0])
Phi = np.array([[ 1.  ,  0.01],
            [ 0.  ,  1.  ]])
Gamma = np.array([[ 0.0001048 ],
              [ 0.02096094]])
Z = np.array([[ 0.0001048 ],
          [ 0.02096094]])
wd = 0
u_new = 0
x1d = 0
x2d = 0
xd = [[0 for col in range(len(x))] for row in range(1000)]
xd[0][0] = 10
xd[1][0] = 0
k = 10
DistPeriodNo1 = 500
FirstPeriod = 1
k=k+1 #Update PeriodNo(so PeriodNo is now equal to No. of current period)
if (k == 100):  #If maximum value of PeriodNo is reached,
    k = 11  #set it to 1
    DistPeriodNo1 = random.randint(11,99) 

if (FirstPeriod == 0):  

    if (k == DistPeriodNo1):    
        wd = random.randint(-1,1)
    else:
        wd = 0
    xd[0][k] = Phi*xd[0][k-1] - Gamma*u_new + Z*wd
    # >>indexerror list index out of range
    xd[1][k] = Phi*xd[1][k-1] - Gamma*u_new + Z*wd
    x1d = xd[0][k]
    x2d = xd[1][k]

Upvotes: 0

Views: 2523

Answers (1)

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19219

To answer your question in the comments about tracebacks (stack traces): running the following

a = [1,2,3]
b = [True, False]
print(a[2])
print(b[2])

produces one answer and one traceback.

>>> 
3
Traceback (most recent call last):
  File "C:\Programs\python34\tem.py", line 4, in <module>
    print(b[2])
IndexError: list index out of range

The traceback shows what line and what code raised the error. People were asking you to copy the last 4 line and paste them into your question (by editing it).

Upvotes: 0

Related Questions