Matlab - binary vector with high concentration of 1s (or 0s)
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.
rand(N,1)<p %# 0 < p < 1
will give you a Nx1 vector with on average N*p ones in it (and other places will be zero) - but on some runs you might get a vector that is different than what you expect (it might have all zeros for example... might be a very low chance, but still non zeros chance).
If you want exactly A ones and B zeros, you can do this:
rand_vec = [ones(A, 1); zeros(B, 1)]; rand_vec = rand_vec(randperm(A+B));
Then you can set A and B to match your needs.
EDIT: Now I understood your question better: Lets say you have a vector p which contains the wanted proportions of 1's in your population, N is the number of elements in each vector.
rand_mat = rand(N, size(p,2)) < repmat(p', [1,N])';
Will give you a Nx(size(p,2)) matrix where column i is a vector with p(i) ones (on average as explained above) and the rest is zeros.
You could use bit-or operations on uniformly distributed binary vectors, I suspect the exact distributions you would end up with would be a little complicated though.