How to code to perform the following task?
Show older comments
Num_DG=2;nPop=50;
Location=[2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;30;31;44;45;46;48;49;77;79;80;82;86;87;88;115;116;117;118;119;120;121;122;123;124;125;126;127;128;129;130;131;132;133;134;135;136;137];
Locations = Location(randi(numel(Location),nPop,Num_DG));
s=[9476,4739,3157,2650];
Sizes=reshape(randperm(s(Num_DG),Num_DG*nPop)-1,nPop,Num_DG);
preposition=[Sizes,Locations];
% OR
min=[0 0 1 1];
max=[4738 4738 137 137];
Num_DG=2;
nVar=2*Num_DG;
VarSize=[1 nVar];
Position=unifrnd(min,max,VarSize);
when the above two task is change by two varaible with complex number like
s=[9476+23i,4739+35i,3157+54i,2650+23i];
max=[4738+34i 4738+342i 137 137]; how to modify the code to perform the same tasks?
modfiy
10 Comments
Torsten
on 25 Jun 2022
"randperm" and "unifrnd" make no sense with complex inputs.
You will have to explain in plain words what you are trying to do.
Assen Beshr
on 25 Jun 2022
Edited: Assen Beshr
on 25 Jun 2022
Well, you'll have to redefine what the task is to be computable with complex values first.
What would you expect the result to be and how would you determine that?
The simplest way one could get a result would be to use abs() of s in its place now -- what any of it would mean or is supposed to mean we have no way to guess....
Steven Lord
on 25 Jun 2022
Rather than having us try to figure out what task you're trying to perform by reverse-engineering the code, please post a description (in words not code) of what you're trying to do. That may make it easier for us to offer suggestions on how to accomplish your task.
Assen Beshr
on 26 Jun 2022
Edited: Assen Beshr
on 26 Jun 2022
dpb
on 26 Jun 2022
You're confusing the size of the arrays with the content of the array.
To generate a complex random variable, generate two random variables with the ranges of the real and imaginary component ranges of choice and combine them with complex()
Torsten
on 26 Jun 2022
Say min = [0 1] and max = [4738+342i 137]
Can you give us a vector of size 1x2 whose element value ranges between min and max values ? You mean the real part of the first element in the vector should range between 0 and 4738 and the imaginary part between 0 and 342 ?
Assen Beshr
on 26 Jun 2022
dpb
on 26 Jun 2022
So, as noted above, a complex PRV is simply
rngReal=[0 4738];
rngImag=[0 342];
c=complex(randi(rngReal),randi(rngImag));
If you have multiple ranges and numbers of rng's to generate for each, you'll want to package the above in some caller-friendly function for ease of coding, but the technique is as shown.
There is no builtin RNG for complex variables to do a single substitution of one function for another; but, you'll have one when you've finished the above.
Of course, you've got to decide the underlying distribution of the real and imaginary parts to decide which generator to use; randi above will return only integer-valued values; rand will generate full-precision doubles but you'll have to scale to the desired range.
Then do as "dpb" suggests.
rng("default")
lb = [254+112i,423+312i];
ub = [588+256i,745+1256i];
lb_real = real(lb(:));
ub_real = real(ub(:));
lb_imag = imag(lb(:));
ub_imag = imag(ub(:));
rand_real = lb_real + rand(size(lb_real)).*(ub_real-lb_real);
rand_imag = lb_imag + rand(size(lb_imag)).*(ub_imag-lb_imag);
random_numbers = (rand_real + 1i*rand_imag).'
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!