I want to create a row matrix of 20 elements (1x20), which consists of 1s and 0s. 1s are generated with rate:r and 0s are generated with rate:(1-r), where r could be any random number between 0 and 1 ( 0<r<1 ).
Any help could be useful. Thanks in advance!

 Accepted Answer

Star Strider
Star Strider on 12 Jan 2015
Edited: Star Strider on 12 Jan 2015
This is one option:
r = 0.5;
idx = randperm(20, ceil(r*20));
rv = zeros(1,20);
rv(idx) = 1;
The logic is relatively straightforward: ‘idx’ uses rendperm to generate a matrix of index positions, then generates ‘rv’ and uses ‘idx’ to determine the r*20 values that are set to 1.
With r = 0.5, this yields:
rv =
Columns 1 through 17
0 1 0 0 1 0 0 1 1 1 1 0 1 0 0 1 1
Columns 18 through 20
1 0 0

4 Comments

Guillaume
Guillaume on 12 Jan 2015
Edited: Guillaume on 12 Jan 2015
Well, if it's really what Konstantinos wants, then I'd use randperm to redistribute the 1 and 0s.
x = ones(1, 20);
x(1:r*20) = 1;
x = x(randperm(20))
However, there are infinitely many r for which r*20 is not an integer. So in the long run:
P(x) == round(20*r)/20 ~= r
Star Strider
Star Strider on 12 Jan 2015
Edited: Star Strider on 12 Jan 2015
I used randperm in my edited code. I initially forgot to include the fix(r*20) in my edited code; it was part of my earlier code. (Now changed to ceil(r*20) since that’s what Konstantinos specified.)
Thanks a lot guys for your time. I really appreciate your help!
Our pleasure!

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 12 Jan 2015
Edited: John D'Errico on 12 Jan 2015
Well, since a test returns a 0 or 1 (i.e., true or false) what is the probability that each element of a set of random samples, taken by the call rand(1,20), is greater than r? What is the probability that they are less than r?
x = (rand(1,20) >= r);

10 Comments

r is the rate, not the value of each element. r will indicate how many ones and zeros I will get in my row matrix.
Guillaume
Guillaume on 12 Jan 2015
Take the time to understand John's questions and actually try his answer.
Apart from the fact that it's returning 10 values instead of 20, this is exactly what you asked for and is the most logical way of obtaining it.
John D'Errico
John D'Errico on 12 Jan 2015
Edited: John D'Errico on 12 Jan 2015
Sigh. I obviously cannot type. 10 and 20 are so close. Fixed now.
As Guillaume said, this does what you asked. If r is the rate, 0<r<1, what is the probabilty that a UNIFORM random variable is less than r? Is greater than r? Recall that rand produces elements in the interval (0,1).
Konstantinos
Konstantinos on 12 Jan 2015
Edited: Konstantinos on 12 Jan 2015
I tried to run it with r=0.5 but I got 11 ones and 9 zeros. I tried once more and I got 9 ones and 11 zeros. Some times it gives me the correct answer but not always.I dont really know the reason for this..
I would agree that it is perhaps the most logical way of obtaining it (I tried that first), but in a vector of only 20 elements, there is no guarantee that ‘exactly’ r*20 will be >= r.
We do not know if Konstantinos wants simple probabilities, in which instance rand(1,20) >= r would yield a vector with a varying number of 1 values, not necessarily r*20, or if r*20 must be 1, and the problem is simply to distribute them randomly.
We need Konstantinos to clarify that for us.
i.e if r = 0.37, then the number of ones I want is: N1 = ceil(r*20)=8 1s and No=(1 - N1)=12 0s
Sigh. I think you do not understand basic probability.
There is NO reason that a fair coin tossed 20 times will always land heads up exactly 10 of them. Similarly, with r = 0.37 for a binomial sample, the probability of receiving exactly 8 ones and 12 zeros is not even the MOST probable event! I'll show this as a simulation, but it is trivial to compute the exact probabilities as I do below for a couple of cases.
hist(sum(rand(1e7,20)<0.37,2),0:20)
grid on
As this simulation shows with a sample size of 1e7 sets of 20 samples, 7 ones is actually more probable than 8.
We can compute the actual probability easily enough. Thus we get 7 ones with probability:
nchoosek(20,7)*0.37^7*(1-0.37)^13
ans =
0.181239550087757
8 ones is indeed less probable.
nchoosek(20,8)*0.37^8*(1-0.37)^12
ans =
0.172968697603594
If you prefer simulation to back up the mathematics...
R = sum(rand(1e7,20)<0.37,2);
sum(R == 7)
ans =
1812235
sum(R == 8)
ans =
1729849
The point is, IF you insist that for a probability of getting a 1 there to be 0.37, AND that the actual number of ones will be 8, then you really do not have a random sample with probability r=0.37.
I think it really is time for you to do some reading. This is probability 101.
@John — That seems to be the crux of the issue. I voted for your answer because in Konstantinos’ original Question, it was not possible to determine what was desired. By my reading, both your Answer and mine are equally valid.
Star - Oh, I don't dispute that you may arguably have a valid answer of the question. Questions are frequently slightly ambiguous, so they can often be read many ways. My patience today is clearly stressed by probabilistically meaningless statements like this:
"if r = 0.37, then the number of ones I want is: N1 = ceil(r*20)=8 1s and No=(1 - N1)=12 0s"

Sign in to comment.

Categories

Find more on Conway's Game of Life in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!