Plotting the probability of drawing a Spade

조회 수: 4 (최근 30일)
Cole  Kleinow
Cole Kleinow 2021년 2월 14일
답변: Image Analyst 2022년 6월 4일
so i need to plot the probability of drawing a spade out of 100 draws (card replaced each time) but i cant quite figure out what to math against what
Deck = [];
Hearts = 0;
Spades = 0;
Diamonds = 0;
Clubs = 0;
n = 100;
Drawn = [];
for rank = 1:13
for suit = 1:4
cardval = [rank suit];
Deck = [Deck, {cardval}];
end
end
for tries = 1:n
Shuffled = Deck(randperm(length(Deck)));
Card = Shuffled(1,1);
Drawn = [Drawn,Card];
if Card{1}(2) == 1
Hearts = Hearts + 1;
elseif Card{1}(2) == 2
Spades = Spades + 1;
elseif Card{1}(2) == 3
Diamonds = Diamonds + 1;
else
Clubs = Clubs + 1;
end
end
SuitsDrawn = [Hearts Spades Diamonds Clubs];
edges = min(Spades):max(Spades);
counts = histc(Spades, edges);
grandTotalSum = sum(counts(:));
normalizedCountSums = counts / grandTotalSum;
plot(edges, normalizedCountSums)
  댓글 수: 1
Cris LaPierre
Cris LaPierre 2021년 2월 15일
How many cards are in a deck? How many of those cards are spades?

댓글을 달려면 로그인하십시오.

답변 (2개)

Vimal Rathod
Vimal Rathod 2021년 2월 19일
Hi,
Probability of drawing spades from 100 draws is still one single value. You can try adding a for loop over the for loop with tries. This way you will get many values of probability of drawing spades from 100 draws. Then you could plot all those points by saving the edges and normalizedCountSums as arrays.
% getting 10 different probability values for
% probability of drawing spades from 100 draws
for set = 1:10
for tries = 1:n
% your code
end
% finding edges and NormalizedCountSums
%concatenate the edges and normalizedCountSums
..
..
end
Hope this helps!

Image Analyst
Image Analyst 2022년 6월 4일
Note that probability is not the count. There will be randomness unless you draw an infinite amount of times.
You can just identify each card with a number, then see how many times that number is drawn.
% Make 100 draws of 1 card each time.
numDraws = 100;
draws = randi([1, 52], numDraws, 1)
% Count each suit.
% Spades are identified as cards 1-13.
% Clubs are identified as cards 14-26.
% Hearts are identified as cards 27-39.
% Diamonds are identified as cards 40-52.
spadeCount = sum(draws >= 1 & draws <= 13)
clubCount = sum(draws >= 14 & draws <= 26)
heartCount = sum(draws >= 27 & draws <= 39)
diamondCount = sum(draws >= 40 & draws <= 52)
% Compute percentages, which is only an approximation of probability.
spadePercentage = 100 * spadeCount / numDraws
clubPercentage = 100 * clubCount / numDraws
heartPercentage = 100 * heartCount / numDraws
diamondPercentage = 100 * diamondCount / numDraws

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by