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..??
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With