abcla abcla - 1 month ago 5
Python Question

Python matrix indexing

I have the following code

l = len(time) #time is a 300 element list
ll = len(sample) #sample has 3 sublists each with 300 elements

w, h = ll, l
Matrix = [[0 for x in range(w)] for y in range(h)]

for n in range(0,l):

for m in range(0,ll):

Matrix[m][n]= x

When I run the code to fill the matrix I get an error message saying "list index out of range" I put in a print statement to see where the error happens and when m=0 and n=3 the matrix goes out of index.

from what I understand on the fourth line of the code I initialize a 3X300 matrix so why does it go out of index at 0X3 ?


You need to change Matrix[m][n]= x to Matrix[n][m]= x