tags:

views:

255

answers:

2

I have a requirement for the generation of a given number N of vectors of given size each consistent of a uniform distribution of 0s and 1s.

This is what I am doing at the moment, but I noticed that the distribution is strongly peaked at half 1s and half 0s, which is no good for what I am doing:

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

The unifrnd function looks promising for what I need, but I can't manage to understand how to output a binary vector of that size.

Is it the case that I can use the unifrnd function (and if so how would be appreciated!) or can is there any other more convenient way to obtain such a set of vectors?

Any help appreciated!

Note 1: just to be sure - here's what the requirement actually says:

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

Note 2: I am generating initial configurations for Cellular Automata, that's why I can have only binary values [0;1].

+2  A: 

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

unidrnd(2,sizeOfVector,1)-1

The other variants are similar.

KennyTM
+1 - thanks! seems to work - but I am having troubles interpreting the expression, could you expand and also explain the difference between what you propose and what I currently have?
JohnIdol
@John: `unidrnd` = Discrete uniform random numbers. `unidrnd(N,m,n)` generates an m*n matrix with random numbers in the range {1,2,...,N}. The `-1` is to put the result back to {0,1,...,N-1}. See http://www.mathworks.com/access/helpdesk/help/toolbox/stats/unidrnd.html.
KennyTM
ok tnx a lot, that explains the use of unidrnd - still trying to understand how that's different from what I have.
JohnIdol
just to clarify my previous comment - what's the difference between using randint(1, 5, [0 1]) and unidrnd(2,1,5)-1 ?
JohnIdol
@John: I don't know on MATLAB, but on Octave 3.0.3 only `unidrnd` exists, and `randint`/`randi` not.
KennyTM
@KennyTM - fair enough :)
JohnIdol
A: 

If you want to get uniformly distributed 0's and 1's, you want to use randi. However, from the requirement, I'd think that the vectors can have real values, for which you'd use rand

%# create a such that each row contains a vector

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

%# if you want, say, 60% 1's 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's and 0's, use
a = rand(N,sizeOfVector) > rand(1);
Jonas
thanks but I can only have 0s or 1s as explained in the question - also isn't randi the same as randint? only difference is randint is deprecated I think :)
JohnIdol
Oh, I see. Yes, randi is the 'modern' version of randint.Also, yes, uniform distribution means all possible values are chosen with equal frequency.
Jonas