I need to write a Matlab program for the card drawing experiment in which you draw a king in the first draw and a queen in the second draw and simulate it varying the number of trials.

8 views (last 30 days)
I have the program to select one card(red Ace) and tried it to modify to select two cards (one king second Queen) but I am not getting the desired result. The program for selecting one card is as below:
n=100; %NUmber of simulations num=rand(n,100); figure, plot(num,'*'); %0:0.25= red hearts, 0.25:0.5=red diamonds, 0.5:0.75=black clubs, %0.75:1=blacck spades %Prob of red hearts face card=0.25-((1/52)*3):0.25...... for i=1:n Upper=0.25; Lower=Upper-(1/52)*3; face_hearts(i,:)=(num(i,:)>=Lower & num(i,:)<=Upper); end sum_face_hearts=sum(face_hearts,2)/100; fprintf('The Simulated probability of getting a face of card in red hearts= %f \n',mean(sum_face_hearts)); fprintf('The Theoretical probability of getting a face of card in red hearts= %f \n',3/52);
Please Help
  3 Comments
Harpreet Pandher
Harpreet Pandher on 21 Oct 2015
Thank you for replying. I need to find P(K1, Q2) in Matlab. That is the probability of drawing two cards at random in which 1st draw is a king and 2nd draw is a queen.
Image Analyst
Image Analyst on 21 Oct 2015
Look at my latest answer below. That's just for one draw. Then you put that code into a loop where you do it a bunch of times.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 20 Oct 2015
Maybe you want randperm() and ismember(). See if this is what you want.
% Cards in an ordered deck are numbered 1 through 52.
% Define which card ID numbers are kings and queens.
queensKings = [12,13, 25,26, 38,39, 51,52]
% Shuffle the deck and get the first two cards.
firstTwoCards = randperm(52,2)
% See if any of these two cards is a Queen or King
ia = ismember(firstTwoCards, queensKings)
if sum(ia) == 1
helpdlg('Found one King or Queen');
elseif sum(ia) == 2
helpdlg('Found two Kings and/or Queens');
end

Image Analyst
Image Analyst on 21 Oct 2015
If you want any king as the first card, and any queen as the second card, this will determine that for one shuffle of the deck and draw of two cards.
% Cards in an ordered deck are numbered 1 through 52.
% Define which card ID numbers are kings and queens.
queens = [12, 25, 38, 51]
kings = [13, 26, 39, 52]
% Shuffle the deck and get the first two cards.
firstTwoCards = randperm(52,2)
% See if the first card is a King.
kingIndexes = ismember(firstTwoCards(1), kings)
% See if the second card is a Queen.
queenIndexes = ismember(firstTwoCards(2), queens)
if kingIndexes == 1 && queenIndexes == 1
helpdlg('Drew a King followed by a Queen');
end

Categories

Find more on Graphics Object Programming 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!