0
a1 = np.random.random_sample(10)

a2 = np.random.random_sample(5)

a3 = np.zeros([10])


for i in range(0,9):

    a3[i] = ((a2[i]+a2[i+1])/2.) * ((a1[i+1]-a1[i])/2.) * (a1[i]*a2[i])

    print(i, a3[i])

I tried to run this simple script and I get the error "IndexError: index out of bounds" ... Please any idea to avoid this error? I understand that the error caused by the number 5, but I can't change that as the a2 array is loaded from a txt (a2= np.loadtxt('data.txt',unpack=True, usecols=[1]) which its length is 5 Please any idea?

1 Answer 1

1

There is only 5 elements in a2 because:

a2 = np.random.random_sample(5) 

but in for loop runs for i > 5 upto 8

for i in range(0,9):

    a3[i] = ((a2[i]+a2[i+1])/2.) * ((a1[i+1]-a1[i])/2.) * (a1[i]*a2[i])
                       ^ index out                                ^ index out  

because you access a2 as a2[i+1] also. So i can be at most 3 try with range(0,4)

EDIT:

To fill five extra 1s do like:

a2.extend(5*[1])
a2
Sign up to request clarification or add additional context in comments.

5 Comments

yes I know the problem... any idea what to do for make this script running? (adding zeros to a2, and increasing its length? )
@user1640255 Added an ides give it a try
I CANT change range(0,9)! ... and also in another part of my script the a2 has length 15, greater that others... whats then? Do I have add zeros to other arrays? Any other idea?
@user1640255 Yes increase a2 size with 0 or 1 although I don't know what is your algo
any idea how to do that? is a secret?

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.