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

What is the best way to generate the number X of random binary vectors of size N with a concentration of 1s (or, symmetrically, 0s) that extends from very low to very high?

Using randint or unidrnd (as in this question ) will generate binary vectors with uniform distributions, which in this case is not what I need.

Any help appreciated!

+3
source share
4 answers

Laserallan is the way to go.

For a vector with 10 elements and 70% units that are randomly distributed, you write

randVec = rand(10,1)<0.7;

EDIT , X- N, ,

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

, randVec - . , , double ,

randVec = double(randVec);
+6

matlab, , , - ( 1 0), 1 0. , 30%, .7 1, - 0.

+3

rand(N,1)<p   %# 0 < p < 1

Nx1 N * p ( ), , , ( , ... , ).

A B , :

rand_vec = [ones(A, 1); zeros(B, 1)];
rand_vec = rand_vec(randperm(A+B));

A B .

EDIT:
: , p, 1 , N - .

rand_mat = rand(N, size(p,2)) < repmat(p', [1,N])';

Nx ( (p, 2)), p (i) ( , ), - .

+2
source

You can use bit operations or operations with uniformly distributed binary vectors, I suspect that the exact distributions you would get would be a bit complicated.

+1
source

Source: https://habr.com/ru/post/1739226/


All Articles