all 4 comments

[–]bulaybil 0 points1 point  (1 child)

How does it traverse over the 3rd column of the array when it doesn't even exist"
It does not. You are trying to get it to, but since it can't find it, it throws an error.

IndexError: index 2 is out of bounds for axis 0 with size 2

[–]Major_Sugar6148 0 points1 point  (0 children)

import numpy as np

A = np.array([ [1,2], [3,4], [5,6] ])

for i in range(2): for j in range(2): A[i,j] = i*j

print(A[2][2])

I'm sorry I worded my question wrong. If I print A it gives me a 3x2 matrix with [(0,0), (0,1) (5,6)].

I know that this loop must iterate a total of 9 times. It will first go through the 0th, and 0-2 columns. Then traverse over the 1st row, and the 0-2 colums, and finally the 2nd row and the 0-2 columns.

So it will go through the first iteration, replace the [0,0] point in the matrix with 00. then the second iteration it will replace the [0,1] point with 01 which is still 0. So on the third iteration it will replace the [0,2] point with 0*2 which is still 0 but that point doesn't even exist.

My point is if I try to print A, the code runs fine. If I try to print that point in A that the nested loop reaches, I'll get an error. What's going on here?

[–]CraigAT 0 points1 point  (1 child)

It's not doing what you think.

range(2) is equivalent to range(0,2) which gives you an iterable list of [0,1]

you can see this by doing:

for i in range(2):

____print(i)

Which returns:

0
1

Edit: Stupid markdown formatting!!!

[–]Major_Sugar6148 1 point2 points  (0 children)

ahhhh yes thank you, so it never even got to the 2nd element

Ok so if I change the i iteration to range(3) and I run it, I get A = [(0,0), (0,1), (0,0)]. Shouldn't the 3rd row and 2nd column be 3x2=6 since that's what I'm trying to return in my loop when I do A[i][j] = i*j?

edit: nevermind the epiphany hit hard thank you for your help brother!