Python matrix indexing
<p>I have the following code</p>

<pre><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):

        x=sample[m]
        Matrix[m][n]= x
</code></pre>

<p>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.</p>

<p>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 ?</p>

<p>Note that indexing in nested lists in Python happens from outside in, and so you'll have to change the order in which you index into your array, as follows:</p>

<pre><code>Matrix[n][m] = x
</code></pre>

<p>For mathematical operations and matrix manipulations, using <code>numpy</code> two-dimensional arrays, is almost always a better choice. You can read more about them <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html" rel="nofollow">here</a>.</p>

python arrays list matrix indexing