0

I have a code which looks like this

 for a in range(0,128):
      for b in range(0,128):
           A = np.zeros((1,3))
           B = np.zeros((1,3))
           for i in range(0,3):
                A[i] = I[a,b,i]

However, it gives me the following error

 A[i] = I[a,b,i]
 IndexError: index 1 is out of bounds for axis 0 with size 1

Thank You, in advance.

1
  • A contains only one row, so A[1] is going to raise an IndexError. Commented Feb 11, 2014 at 14:34

1 Answer 1

4

np.zeros((1, 3)) creates an array with one "row" and three "columns":

array([[ 0.,  0.,  0.]]) # note "list of lists"

If you want to index straight into the columns, you can simply create the array as:

A = np.zeros(3)

and get

array([ 0.,  0.,  0.])

Then your loop will work as currently written.

Alternatively, you will need to index into the row first:

for index in range(3):
    A[0, index] = ...
Sign up to request clarification or add additional context in comments.

1 Comment

numpy arrays are indexed A[0, index].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.