Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB, how to change loop index inside for loops [duplicate]

Tags:

matlab

I have a loop like this:

b = 1;
for c = 1 : 10;
  if b == 1
      c = 1 + 3
  end
end

What do I need to do to make to change c? Because as I read through the help, MATLAB resets the loop counter c after it reaches end.

Is there any way to change the value of the loop counter from within a for loop?

like image 698
Adrian Hartanto Avatar asked Jan 29 '26 07:01

Adrian Hartanto


1 Answers

you could use a while loop instead of a for loop.

something like (I'm guessing you want to add 3 to c otherwise c = 4 could replace that line below)

b = 1;
c = 1;
while(c < 10)
    if b == 1
        c = c + 3
    end
end
like image 169
smitec Avatar answered Jan 31 '26 06:01

smitec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!