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

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

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
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

댓글을 달려면 로그인하십시오.

 채택된 답변

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

"You can always convert a decimal number to binary using the dec2bin function." - except not floating point numbers like Ayesha wants. Only positive integers work with dec2bin():
str = dec2bin(d) returns the binary representation of d as a string. d must be a nonnegative integer smaller than 2^52.
thanx alot sabsatian let me try this will update you then if it works
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
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Numeric Types에 대해 자세히 알아보기

질문:

2015년 2월 3일

댓글:

2015년 2월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by