MATLAB: Basic probability question

probability

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

Best Answer

  • 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.