Home > front end >  Can somebody explain to me why numpy indexing fails when using a 1 row matrix?
Can somebody explain to me why numpy indexing fails when using a 1 row matrix?

Time:08-21

To keep it simple:

points_3D_db.shape = (2816, 4233, 3)
index = array([33, 2860], dtype=int16)

These works:

points_3D_db[33, 2860] = array([ 1.54911746, -2.87904632,  7.43229437])
points_3D_db[(33, 2860)] = array([ 1.54911746, -2.87904632,  7.43229437])
points_3D_db[index[0], index[1]] = array([ 1.54911746, -2.87904632,  7.43229437])

This doesn't not work:

points_3D_db[index] = *** IndexError: index 2860 is out of bounds for axis 0 with size 2816

Now why numpy would throw this error ?

CodePudding user response:

You need to use a tuple to index:

points_3D_db[tuple(index)]) = array([ 1.54911746, -2.87904632,  7.43229437])

Example:

points_3D_db = np.zeros((2816, 4233, 3))
index = np.array([33, 2860], dtype=np.int16)

points_3D_db[tuple(index)] = [1,2,3]

print(points_3D_db[index[0], index[1]])
# [1. 2. 3.]
  • Related