Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I need to loop backwards from i=n-2 to i = 0 to code this 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
range(n - 2, 0)
n-2
-1
range(n - 2, -1, -1)
0
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.
Add a comment
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
range(n - 2, 0)will count up fromn-2to-1. If you want to go backwards, you should dorange(n - 2, -1, -1)so it counts backwards fromn-2to0(the last index isn't included)