Plotting the probability of drawing a Spade
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Antworten (2)
Vimal Rathod
am 19 Feb. 2021
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!
0 Kommentare
Image Analyst
am 4 Jun. 2022
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!