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!