how to loop back to the start of a for loop in the middle

70 Ansichten (letzte 30 Tage)
Morgan Clendennin
Morgan Clendennin am 31 Aug. 2017
Beantwortet: Walter Roberson am 31 Aug. 2017
I hope the question I am asking makes sense but I will try and explain it as best I can. If you have any further questions please ask. I am trying to come up with a piece of code that will take certain information that is fed to it, do what I tell the function to do and then if certain conditions are met, it will start running again at a specified part in the code. For example, let's say I have x = 15. Now let's say I define a, b, c, d, and e to be as follows. a = 5, b = 6, c = 8, d = 9, and e = 12. I want to run a piece of code that will do the following steps:
  1. 1. Check to see if x - a >= 0
  2. 2. If x - a >= 0, x = x - a.
  3. 3. Check to see if x - a is still >= 0
  4. 4. If x - a >= 0, x = x - a.
  5. 5. if x - a < 0, check to see if x - b >= 0.
  6. 6. if x - b >= 0, x = x - b.
  7. 7. Check to see if x - b is still >= 0.
  8. 8. if x - b >= 0, repeat steps 4 - 7 using b and c now.
  9. 9. repeat the previous steps 1- 8 for c, d and e.
  10. 10. if x - e < 0, do nothing or values remain the same.
I want these steps to work for any amount of re-loops meaning if x = 15 and a = 5 then x - a will occur 3 times but if x = 10 it will occur twice and if x = 20, it will occur 4 times, and so on. I don't want this to be a piece of code where it checks if the stated conditions above are met a pre-defined number of times but rather it will check it dynamically and will re-enter the loop as many times as necessary. If this is needed to re-enter the beginning of the loop and automatically bypass the conditions it doesn't meet that is acceptable. By this I mean let's say we're on step 6 above. We do step 6 and then check in step 7. If we need to re-loop back to step 1 and automatically bypass steps 1 - 4, then that's fine.

Antworten (1)

Walter Roberson
Walter Roberson am 31 Aug. 2017

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by