iterating a loop many times

1 view (last 30 days)
Erik Lee
Erik Lee on 13 Oct 2015
Commented: Star Strider on 13 Oct 2015
  • I'm not sure how to iterate this loop 1,000 times:
  • ---a=randperm(20)
  • LOOP STARTS:
  • --- code that swaps 2 elements in 'a' randomly to make a guess 'amaybe'
  • --- if fun(amaybe)>fun(a)
  • anew=amaybe; (guess is kept)
  • elseif fun(amaybe)<fun(a)
  • anew=a; (guess is rejected)
  • end
  • LOOP ENDS
I need to make 1,000 guesses (swapping 2 elements each time) until the final guess should have the highest fun value. How do I iterate the loop that many times? And should I use a for-loop instead of if/elseif?

Accepted Answer

Star Strider
Star Strider on 13 Oct 2015
I would use a for loop because (1) it seems you need to do 1000 iterations and (2) the comparison operation (whatever it is that ‘fun’ does) doesn’t affect the number of iterations.
  4 Comments
Erik Lee
Erik Lee on 13 Oct 2015
Thanks, you're a total lifesaver for a coding noob like me :)
Star Strider
Star Strider on 13 Oct 2015
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!