Clear Filters
Clear Filters

Generating random complex numbers between -0.5 to 0.5 in Gatbx

60 views (last 30 days)
i am trying to use the gatbx for a problem . in which functions like bs2rv , crtbase, crtbp, crtrp, Objfun, ranking , select, recombine , reins these functions are present.
now in the main
NIND = 40; % Number of individuals per subpopulations
MAXGEN = 15; % maximum Number of generations
GGAP = .9; % Generation gap, how many new individuals are created
NVAR = 32; % Number of variables
PRECI = 8; % Precision of binary representation
% Build field descriptor
FieldD = [rep([PRECI],[1, NVAR]); rep([-0.5-0.5i;0.5+0.5i],[1, NVAR]);...
rep([1; 0; 1 ;1], [1, NVAR])];
% Initialise population
Chrom = crtbp(NIND, NVAR*PRECI);
Phen = bs2rv(Chrom,FieldD);
what i am trying to do is to generate some random complex numbers between -0.5 to 0.5 .
in the FieldD vector i am specifying the upper bound and lower bound for my variable where i have written
rep([-0.5-0.5i;0.5+0.5i],
but this is giving me numbers like
-0.3863 - 0.3863i 0.3510 + 0.3510i
0.2961 + 0.2961i 0.2373 + 0.2373i
-0.1196 - 0.1196i 0.1039 + 0.1039i
0.2961 + 0.2961i -0.3706 - 0.3706i
-0.4843 - 0.4843i -0.4882 - 0.4882i
-0.3706 - 0.3706i -0.2412 - 0.2412i
-0.0373 - 0.0373i 0.3471 + 0.3471i
0.4176 + 0.4176i -0.4294 - 0.4294i
-0.1745 - 0.1745i 0.3784 + 0.3784i
-0.3706 - 0.3706i -0.2451 - 0.2451i
-0.4882 - 0.4882i 0.4098 + 0.4098i
0.0412 + 0.0412i 0.2882 + 0.2882i
0.3157 + 0.3157i -0.4725 - 0.4725i
-0.3863 - 0.3863i 0.3510 + 0.3510i
-0.4647 - 0.4647i 0.0098 + 0.0098i
0.4137 + 0.4137i 0.2451 + 0.2451i
-0.1353 - 0.1353i 0.3000 + 0.3000i
0.3000 + 0.3000i 0.4373 + 0.4373i
-0.0373 - 0.0373i -0.4333 - 0.4333i
0.1471 + 0.1471i -0.4373 - 0.4373i
in my Phen vector. this is missing numbers like -0.4373 + 0.4373i or +0.4373 - 0.4373i what can i do to make this happen ..
  3 Comments
Hamza
Hamza on 19 Jan 2015
this is a replicate function, basically this FieldD vector is used for encoding the real valued variables to the 'Phen' through Phen = bs2rv(Chrom,FieldD);while chromosome is the binary string and FielD is specifying how to do it. included the precision= how many bits should be used to represent one variable. lb=lower bound, up=uperbound, up and lb is the range of the my variable. like presently i want that my variable should be between -0.5 and 0.5 complex numbers. if i want to do it using this FieldD i have to specify an up and lb. i tried rep([-0.5-0.5i;0.5+0.5i]; this one is not giving me complex numbers like +0.5-0.5i,and -0.5+0.5i. i want to solve this i am attaching the functions for you.
dpb
dpb on 19 Jan 2015
Not knowing much about what this is really doing, just looking the routine doesn't seem tailored to handle complex variables.
Looks to me like you would either use Roger's solution or if you think you must use these routines, modify the routine to specifically handle complex.
The question then becomes is it simply a random sign between Re,Im parts you care about or do you want a "real" PRV over the complex plane?
b)

Sign in to comment.

Answers (2)

Roger Stafford
Roger Stafford on 19 Jan 2015
(rand(20,2)-.5) + (rand(20,2)-.5)*1i ;

Hamza
Hamza on 19 Jan 2015
yes you are right . the code you mentioned is giving me 20 complex numbers within this range . but in my situation i have to specify a range . i want to do it using that range. this is a replicate function, basically this FieldD vector is used for encoding the real valued variables to the 'Phen' through Phen = bs2rv(Chrom,FieldD);while chromosome is the binary string and FielD is specifying how to do it. included the precision= how many bits should be used to represent one variable. lb=lower bound, up=uperbound, up and lb is the range of the my variable. like presently i want that my variable should be between -0.5 and 0.5 complex numbers. if i want to do it using this FieldD i have to specify an up and lb. i tried rep([-0.5-0.5i;0.5+0.5i]; this one is not giving me complex numbers like +0.5-0.5i,and -0.5+0.5i. i want to solve this i am attaching the functions for you.

Community Treasure Hunt

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

Start Hunting!