1

I am using for loops to iterate through the indices in a NumPy zeros array and assign some indices with the value 0.5. At the moment, my code returns the error message:

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

Below is a simplified version of my code which reproduces the error.

import numpy as np

Z = np.zeros((1560, 1560))
linestart = {1: [175], 2: [865]}
noycuts = 2

cutno = int(0)
for i in range(noycuts):
    cutno = cutno + 1
    xstart = linestart[cutno]
    ystart = 0
    for j in range(1560):
        Z[xstart][ystart] = 0.5
        ystart = ystart + 1

I've checked questions from people with the same error code, although these issues seem to stem from how the array was originally called; I don't think this is my problem.

Can anyone see the flaw in my code that is causing the error message?

I hope I have provided enough information.

Thanks in advance.

2
  • 1
    Your ystart starts with 1. Shouldn't it start with 0? Commented Jan 30, 2017 at 14:29
  • It did originally, although I changed it to 1 to see if it made any difference and I've forgotten to change it back. I've edited it back to 0. Thanks. Commented Jan 30, 2017 at 14:32

2 Answers 2

1

Edit:

My original answer was:

Replace

Z[xstart][ystart] = 0.5

with

Z[xstart, ystart] = 0.5

But actually, the problem is, that your xstart is an array. Leave your original code, but replace

linestart = {1: [175], 2: [865]}

with

linestart = {1: 175, 2: 865}

or, better:

linestart = [175, 865]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I thought it would be something simple! Could you possibly explain why this works? I have for loops earlier in the code using the "Z[xstart][ystart]" syntax and these work perfectly fine.
0

With linestart = {1: [175], 2: [865]} you define a dict containing lists with single entrys. I belive you actually want the dict to contain ints. Also ystart should start with zero. Does the following do what you want:

import numpy as np

Z = np.zeros((1560, 1560))
linestart = {1: 175, 2: 865}
noycuts = 2

cutno = 0
for i in range(noycuts):
    cutno += 1
    xstart = linestart[cutno]
    ystart = 0
    for j in range(1560):
        Z[xstart][ystart] = 0.5
        ystart = ystart + 1

Also consider the following which is a shorter version:

for cutno,xstart in linestart.items():
    for ystart in range(Z.shape[1]):
        Z[xstart][ystart] = 0.5

2 Comments

Yes this does exactly what I want. In this particular instance the dict contains only single entries per key, however, in the full code, I have multiple dicts which are added to via raw_input and sometimes contain more than one entry per key; these dicts require the use of lists and I have sloppily continued to use the same format for this dictionary also. Thank you for pointing this out.
Also, thanks for the shorter version, it's very helpful.

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.