Basic probability question
2 次查看(过去 30 天)
显示 更早的评论
Could some please let me know the matlab command to generate a random sequence of 1 and 2 if the P(1)=0.6 and P(2)=0.4.
If we use the command 'randint(1,10,[1,2])' it generates 1,2 with equal probability.
Expecting a response soon. Thanks in advance
Regards
0 个评论
采纳的回答
Daniel Shub
2011-7-5
x = rand(1, 10); % Makes x have values between 0 and 1.
y = ones(size(x)); % Start off with all ones
y(x > 0.6) = 2; % Change all the values of y, for which x is greater than 0.6 to 2.
0 个评论
更多回答(4 个)
Oleg Komarov
2011-7-5
% Preallocate 1s
Out = ones(100,1);
% 40% shuld be 2s
num = round(numel(Out)*.4);
% Randomly scatter the 2s among the 1s
Out(randi([1 numel(Out)],num,1)) = repmat(2, num,1);
0 个评论
Royi Avital
2011-7-5
Uniform distribution is always a good point to start from. Then just divide the range [0 1] to whatever ratio you'd like according to the distribution you're after.
numElements = 100;
uniformDist = rand(numElements, 1);
outputDist = zeros(numElements, 1);
outputDist(uniformDist <= 0.6) = 1;
outputDist(uniformDist > 0.6) = 2;
0 个评论
David Young
2011-7-5
You can make a function that you can use instead of rand, like this:
p1 = 0.6; % probability of a 1 in the output
rand_special = @(m,n) (rand(m,n) > p1) + 1;
Then you can call the function to get an array of the size you want:
rand_special(1, 10)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!