What's the best way to generate a number X of random binary vectors of size N with concentration of 1s (or, simmetrically, of 0s) that spans from very low to very high?
Using randint or unidrnd (as in this question) will generate binary vectors with uniform distributions, which is not what I need in this case.
Any help appreciated!
Laserallan's approach is the way to go.
For a vector with 10 elements and 70% ones that are randomly distributed, you write
randVec = rand(10,1)<0.7;
EDIT If you want X vectors of length N, with increasing amount of ones, you write
thresVec = linspace(0,1,X); %# thresholds go from 0 (all 0) to 1 (all 1)
randVec = bsxfun(@lt,rand(N,X),threshVec); %# vectors are per column
Note that randVec is a logical array. Depending on what you want to do with it, you may want to convert it to double like so
randVec = double(randVec);
I'm no expert on matlab but an approach that should be pretty simple to implement is generating vectors of floating point random numbers (preferably in the range 1 to 0) and then set values above a certain threshold to 1 and below to 0. Let's say you want 30% ones, you set all values above .7 to 1 and the ones below to 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With