Question

I am trying to create a GA population of integers in MATLAB, where each individual is a string of random numbers 1-8, without repeating any number. I know the following creates a random arrangement of numbers 1 -8:

A = randperm(8)

I would like to know how to create a function that creates a population of such a kind.

Was it helpful?

Solution

The way randperm used to work was with sort and rand. We can do it the same way, but with multiple columns to get a population:

>> N = 8;  % length of random string
>> P = 10; % population
>> [~,AA]=sort(rand(N,P))
AA =
     5     6     7     6     1     4     7     8     3     2
     8     7     6     5     4     2     4     3     2     3
     6     3     1     8     8     5     8     1     5     8
     3     4     8     1     7     6     2     7     4     7
     4     2     5     4     6     1     1     4     1     4
     2     5     2     3     3     8     3     6     6     6
     1     1     3     2     2     3     6     5     8     1
     7     8     4     7     5     7     5     2     7     5
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top