How to update counter variable in a for loop
问题 Say I have a for loop that counts from 10 down to 1 with counter k . In the loop, when k becomes 5, I make k=4 . When I output k in each loop, I expected it would skip the 4 like so: 10 9 8 7 6 5 3 2 1 Instead I got all the numbers from 10 down to 1, it did not skip the 4. How can I make it so that it skips 4? for k=10:-1:1 if i==5 k=i-1; end end 回答1: You cannot modify the loop index from within the loop in MATLAB. Your two options are to omit that that index value prior to the loop numbers =