Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab - binary vector with high concentration of 1s (or 0s)

Tags:

random

matlab

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!

like image 575
JohnIdol Avatar asked Feb 04 '26 18:02

JohnIdol


2 Answers

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);
like image 158
Jonas Avatar answered Feb 06 '26 06:02

Jonas


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.

like image 25
Laserallan Avatar answered Feb 06 '26 08:02

Laserallan