1

I need to loop backwards from i=n-2 to i = 0 to code this math formula:

math formula

for i in range(n-2,0):
    X[i] = Y[i]
    for m in range(i+1,n):

        X[i] = X[i] - T[i,m] * X[m]

It doesn't work, what am I doing wrong?

All numbers from numpy arrays

1
  • range(n - 2, 0) will count up from n-2 to -1. If you want to go backwards, you should do range(n - 2, -1, -1) so it counts backwards from n-2 to 0 (the last index isn't included) Commented Nov 28, 2021 at 8:25

1 Answer 1

1

if you want to loop backward you can use the for loop like following

range(start, end, step)

the step is 1 by default. in your case, you have to specify the decrement in order the loop the work.

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.