Create a row Matrix
Show older comments
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
More Answers (1)
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
Konstantinos
on 12 Jan 2015
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
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.
John D'Errico
on 12 Jan 2015
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
on 12 Jan 2015
Edited: Konstantinos
on 12 Jan 2015
Star Strider
on 12 Jan 2015
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.
Konstantinos
on 12 Jan 2015
John D'Errico
on 13 Jan 2015
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.
Star Strider
on 13 Jan 2015
@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.
John D'Errico
on 13 Jan 2015
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"
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!