1

I have written a simple function which iterates over a numpy array with some fixed indices.

def compute_V(i,j,nA, nB,V):
    Vijkl = np.zeros((i,j,nA,nB))
    for k in range(nA):
        for l in range(nB):
            Vijkl[i,j,k,l] = V[i,j,k,l] + 3

    return Vijkl

I am getting the following error back: IndexError: index 1 is out of bounds for axis 0 with size 1

What am I doing wrong?

The matrix V has a shape: (1, 2, 1, 2) and looks:

[[[[-0.00009 -0.00001]]

  [[-0.00001 -0.00001]]]] 

i = 0, j = 0, nA = 1, nB = 2 

Looks the loop goes over one iteration:

i,j,k,l,V:  0 0 0 0 -9.39073120245e-05

then throws the error.

6
  • IndexError means that you are trying to get an element that doesn't exist in the List. For example imagine if your trying to get the forth element of a list, even though the list only has three elements. Commented May 25, 2018 at 15:23
  • What's the shape of V? Could be that it's a mismatch with nA or nB? Commented May 25, 2018 at 15:32
  • V is (1, 2, 1, 2), nA = 1, nB =2 Commented May 25, 2018 at 15:34
  • Not really related to your question, but if all you do is to fill 3 in some of the elements there are much more elegant ways to do so. Commented May 25, 2018 at 15:54
  • @anishtain4 It just an example for more complicated task. Commented May 25, 2018 at 16:17

1 Answer 1

1

The shape of Vijkl is (i, j, nA, nB), so the maximum limits for each dimension are (i-1, j-1, nA-1, nB-1). You are exceeding the maximum range of the first two dimensions.

Sign up to request clarification or add additional context in comments.

Comments

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.