3

This is my attempt of a simple example (it seems pointless) but the idea is bigger than this simple code.

During a for loop, if something happens, I want to skip this step of the for loop and then add an extra step on the end.

  1. I am trying to create a list of numbers, that do not include the number 8.

  2. If the code creates an 8, this will mean that exitflag is equal to 1.

  3. Can I adapt this program so that if the exitflag=1, the it will remove that result and add another loop.

The code:

for i = 1:1000
    j = 1+round(rand*10)
    if j == 8
        exitflag = 1
    else
        exitflag = 0 
    end
    storeexit(i)=exitflag;
    storej(i)=j;
end
sum(storeexit)

I would ideally like a list of numbers, 1000 long which does not contain an 8.

3
  • @Jean-ClaudeArbaut its not about the numbers here, I'm giving a simple example.... Commented Oct 30, 2019 at 14:50
  • 1
    I think what you want to do is repeat the same step of the loop until the result is not an 8. Is that right? Commented Oct 30, 2019 at 14:51
  • 1
    @CrisLuengo absolutely Commented Oct 30, 2019 at 14:53

2 Answers 2

4

If what you want to do is 1000 iterations of the loop, but repeat a loop iteration if you don't like its result, instead of tagging the repeat at the end, what you can do is loop inside the for loop until you do like the result of that iteration:

stores = zeros(1000,1); % Note that it is important to preallocate arrays, even in toy examples :)
for i = 1:1000
    success = false; % MATLAB has no do..while loop, this is slightly more awkward....
    while ~success
       j = 1+round(rand*10);
       success = j ~= 8;
    end
    storej(i) = j; % j guaranteed to not be 8
end
Sign up to request clarification or add additional context in comments.

5 Comments

@LiorCohen this is what I meant with the greatest respect
Thanks, its got me thinking but not exactly what I was looking for, it doesn't solve what I was looking for, as in the actual example something else has to run before I know that j = 8 and I cant just assume that j might be 8.
@user30609: Could you please elaborate? Maybe edit your question to clarify this point? The initialization here (j=8) is just to get the while loop to run once. Inside the while loop, you run your code, and the while loop repeats until you are happy with the result of the code. Usually I'd write do <something> while <condition>, but MATLAB doesn't know that syntax. So instead I need to initialize j to the failure state, so that the while loop runs at least once. I rewrote the code to make this point more obvious.
Ok, i think I come with a solution, but I need j to equal a number before I can proceed?
@user30609: Maybe the rewritten code is easier to adapt to your particular situation? It's the same logic in principle.
-1

No.

With for loop, the number of loops is determined on the loop start and it is not dynamic.

For doing what you want, you need to use a while loop.

1 Comment

Surely this is a poor answer. No with an explanation of no, wouldn't it be helpful for the OP and the rest of the community if you provided an example of your method working.

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.