Randomly generate a matrix of zeros and ones with a non-uniform bias

Can anyone help me to generate a matrix of zeros and ones randomly without uniformly distributing like the function of "randint". Instead with the number of ones must be larger than zeros in an array.
For example, a matrix like this:
A= [ 1 1 1 1 0 1 0, 1 0 1 1 1 1 1, 1 1 0 1 1 0 0]

 채택된 답변

Matt Fig
Matt Fig 2011년 2월 11일
Like this?
A = rand(1,21)>.3 % Increase number for less ones, decrease for more.

댓글 수: 2

Hi Matt, for the time being, i'm using your solution as it could give the ones more than zeros.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Jos (10584)
Jos (10584) 2011년 2월 11일
The following gives you a matrix with exactly a certain number of 1's
sizeR = [3 4] ;
num1 = 8 ;
R = zeros(sizeR) % set all to zero
ix = randperm(numel(R)) % randomize the linear indices
ix = ix(1:num1) % select the first
R(ix) = 1 % set the corresponding positions to 1
By the way, if the only values in R are 0 and 1 than logical arrays may be worthwhile to explore, as they are less memory consuming than double arrays.

댓글 수: 7

Thanks, this was very helpful. Is there a way to make these ones being adjacent elements? example for 6x3 matrix with two 1's: [1 1 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0]
Thanks, this was very helpful. Is there a way to make these ones being adjacent elements? example for 6x3 matrix with two 1's: [1 1 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0]
Thanks, this was very helpful. Is there a way to make these ones being adjacent elements? example for 6x3 matrix with two 1's: [1 1 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0]
Thanks, this was very helpful. Is there a way to make these ones being adjacent elements? example for 6x3 matrix with two 1's: [1 1 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0]
Thanks, this was very helpful. Is there a way to make these ones being adjacent elements? example for 6x3 matrix with two 1's: [1 1 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0]
Thanks, this was very helpful. Is there a way to make these ones being adjacent elements? example for 6x3 matrix with two 1's: [1 1 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0]
Thanks, this was very helpful. Is there a way to make these ones being adjacent elements? example for 6x3 matrix with two 1's: [1 1 0 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0]

댓글을 달려면 로그인하십시오.

0 + (rand(7,3) > 0.4)

댓글 수: 1

Not that matter, but in light of the recent discussion, in order to generate a 0.4 probability of zeros, the more accurate RAND() cut is:
s = 2^(-53)
cut = 0.4*(1-s)+s/2
r = rand > cut

댓글을 달려면 로그인하십시오.

Nurul
Nurul 2011년 2월 11일

0 개 추천

Thanks for your help. Its helped me a lot.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2011년 2월 11일

댓글:

2020년 5월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by