iterating a loop many times
1 view (last 30 days)
Show older comments
- 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?
0 Comments
Accepted Answer
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
More Answers (0)
See Also
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!