Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: Updating max count in a loop doesn't work

Tags:

loops

matlab

I have executed this simple loop script in MATLAB

clc;clear; close all;
m = 100;
for i = 1:m
    if(i == 2)
        m = 1000;
    end
end 

and found, that 'i' loops only till '100' BUT NOT '1000'. Why..??

like image 547
Gautam Krishna Avatar asked Dec 18 '25 05:12

Gautam Krishna


1 Answers

The statement for i=1:m assigns the array 1:m to the list of values the operator will take on during the loop. This happens when the loop starts executing (note: you can use any array, and it'll be worked through column by column; for letter='abcde';fprintf('%s\n',letter);end works fine).

If you want to adjust how often your loop will be iterated through, I recommend using a while loop:

ct = 1;
maxIterations = 100;
success = false;
while ~success
   fprintf('iteration %i/%i\n',ct,maxIterations);
   ct = ct + 1;
   if ct == 2
      maxIterations == 1000;
   end

   if ct > maxIterations
       success = true;
   end
end
like image 99
Jonas Avatar answered Dec 21 '25 05:12

Jonas