Uniform distribution of binary values ​​in Matlab

I have a requirement for generating a given number N of vectors of a given size, each of which corresponds to a uniform distribution of 0s and 1s.

This is what I am doing at the moment, but I noticed that the distribution hit a maximum at half 1 s and 0, which is not suitable for what I am doing:

a = randint(1, sizeOfVector, [0 1]);

The function looks promising for what I need, but I cannot figure out how to output the binary vector this size. unifrnd

In this case, I can use the function unifrnd(and if so, how would you rate it!), Or maybe there is some other more convenient way to get such a set of vectors?

Any help appreciated!

Note 1 : just to be sure - this is what it actually says:

randomly select N vectors of a given size that are uniformly distributed over [0; 1]

Note 2 . I am creating initial configurations for Cellular Automata, so I can only have binary values ​​[0; 1].

+1
source share
2 answers

To create 1 random vector with elements from {0, 1}:

unidrnd(2,sizeOfVector,1)-1

Other options are similar.

+2
source

If you want to get evenly distributed 0 and 1, you want to use randi . However, from this requirement, I would think that vectors can have real values, for which you would use rand

%# create a such that each row contains a vector

a = randi(1,N,sizeOfVector); %# for uniformly distributed 0 and 1's

%# if you want, say, 60% 1 and 40% 0's, use rand and threshold
a = rand(N,sizeOfVector) > 0.4; %# maybe you need to call double(a) if you don't want logical output

%# if you want a random number of 1 and 0's, use
a = rand(N,sizeOfVector) > rand(1);
0
source

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


All Articles