MATLAB: Need help generating GA population

chromosomesgapopulation

I am new to GA and there is something that not clear to me. I want to generate a population of k chromosomes with each chromosome length equal to Nt. Each gene in each chromosome is randomly either a '1' or '0'. For every '1' found in a chromosome i want to assign a random number between 1 and 15 for that index position. I need help generating this population.

Best Answer

Try this:
InitPop = randi([0 1], k, Nt);
InitPop(InitPop == 1) = randi([1 15], 1, nnz(InitPop));
This initially creates ‘InitPop’ as a matrix of [0 1] values, tne substitutes the 1 values with integers from 1 to 15.