Why does my code bug down during while loop?

1 view (last 30 days)
Hello everyone,
I am working on an genetic algorithm optimization problem and I have created a function which should create inital random values of variables. There are 661 instances each requiring a set of randomly generated variables. For some odd reason the code quickly generates first 100, but totally bugs down on 101-st. When I try the function outside of the entire code and for the 101-st particular instance everything works fine. But in the loop it simply does not work.
I have tried two options:
  1. unifrnd which, after I terminate code execution, gives "Operation terminated by user during unifrnd"
  2. for loop which also bugs down on the line where for is written
Does anyone understand why is this happening?
I have attached everything you need to try it out.

Answers (1)

Benjamin Thompson
Benjamin Thompson on 1 Jul 2022
Have you tried the Profiler tool with your code? This can quickly highlite which parts take the most time. In this case line 24 of population.m uses quite a bit of the time. It is always recommended to create vectors with the desired size at the beginning instead of adding to its size on each iteration of a loop. The rand function also accepts size arguments and this will execute much faster than pulling scalar random numbers one by one.
x = zeros(N, 1);
rvalues = rand(N,1);

Community Treasure Hunt

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

Start Hunting!

Translated by