Plotting the probability of drawing a Spade

3 次查看(过去 30 天)
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)

回答(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