How to generate random binary numbers matrix with some decimal number constraint

4 visualizzazioni (ultimi 30 giorni)
for example i have a variable -3.0<=x<= 12.1, i want to have values of this x in binary form with in this decimal limit
  2 Commenti
Ayesha
Ayesha il 3 Feb 2015
Let me elaborate my problem i have to max this function max f(x1,x2)=21.5+x1sin(4pix1)+x2sin(20pix2) where -3<=x1<=12.1 4.1<=x2<=5.8 using genetic algo i have to maximize and for ga i have to encode these x1 x2 into binary strings. i want to genrate an initial population of x1 and x2 in binary form with in this decimal range
Ayesha
Ayesha il 3 Feb 2015
P = randi([0,1],5,33) % Generate a 5-by-33 random binary matrix. c=P(1:5,1:18); % x1 values extraction rom random population d=P(1:5,19:33); x1=bi2de(c); %conversion of x1 values to decimal x2=bi2de(d); f=zeros(1,5); for i=1:5 f(i)=21.5+x1(i,1)*sin(4*360*x1(i,1))+x2(i,1)*sin(20*360*x2(i,1)); f(1,i)=f(i); end f %evaluation of fitness values F=sum(f) %total fitness of the population right now i have coded this.but the problem with this code is the random binary numbers that are being generated theor conter decimal parts are out of constaint of x1 and x2.while i want them to be in this constaint

Accedi per commentare.

Risposta accettata

Sebastian Castro
Sebastian Castro il 3 Feb 2015
You could use the randi function to generate a random integer in a range, and then perform scaling and offset to convert it to what you want.
For example, take the following command
x = 0.1*randi(20) + 2
  • randi(20) generates a number from 1 to 20
  • 0.1*randi(20) generates a number from 0.1 to 2, in increments of 0.1
  • 0.1*randi(20) + 2 generates a number from 2.1 to 4, in increments of 0.1
You can apply a similar principle to get the range you want.
Also, if you want to generate multiple numbers, you can use the second argument of the function. For example, to generate a 2-by-3 array of the same random numbers:
x = 0.1*randi(20,[2 3]) + 2
Finally, you mentioned binary numbers. You can always convert a decimal number to binary using the dec2bin function.
- Sebastian
  4 Commenti
Ayesha
Ayesha il 3 Feb 2015
Let me elaborate my problem i have to max this function max f(x1,x2)=21.5+x1sin(4pix1)+x2sin(20pix2) where -3<=x1<=12.1 4.1<=x2<=5.8 using genetic algo i have to maximize and for ga i have to encode these x1 x2 into binary strings. i want to genrate an initial population of x1 and x2 in binary form with in this decimal range
Sebastian Castro
Sebastian Castro il 3 Feb 2015
Thanks, Image Analyst! That's right, you would have to use dec2bin on what is known as the "stored integer" -- i.e. just the random integer generated before the scaling and offset.
Regarding Genetic Algorithms, do you have the Global Optimization Toolbox? You can use the GA function, and GAOPTIMSET to specify the InitialPopulation property.
As long as you constrain your stored integer to be between (for example) 1 through 20, you can perform the offset (e.g. 0.1) and scaling (e.g. 2) within the fitness function itself.
- Sebastian

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by