Effacer les filtres
Effacer les filtres

How can I print the outcomes of a binomial distribution experiment in Matlab ?

2 vues (au cours des 30 derniers jours)
I want to print the outcome of 3 times coin tossing by Matlab, could you please help me to write a code for this experiment, which explained in details by this link:
  2 commentaires
John
John le 20 Mai 2016
rand(3,1) > 0.5
Susan Arnold
Susan Arnold le 20 Mai 2016
I need the outcomes will be letters like HHH, HTH.....

Connectez-vous pour commenter.

Réponse acceptée

Di Zhu
Di Zhu le 20 Mai 2016
Modifié(e) : Di Zhu le 20 Mai 2016
I hope this helps:
% use binary numbers to generate all possible outcomes
var1 = cell(8,1);
for i = 1 : 8
var1{i} = dec2bin(i - 1);
if length(var1{i}) == 1
var1{i} = strcat('00',var1{i});
elseif length(var1{i}) == 2
var1{i} = strcat('0',var1{i});
end
end
display(var1);
% use 'H' and 'T' to replace '0' and '1'
var2 = zeros(8,1); % for counting the number of heads of each outcome
var3 = cell(8,1); % for storing the chars 'H' and 'T'
for i = 1 : 8
for j = 1 : 3
if var1{i}(j) == '0'
var2(i) = var2(i) + 1;
var3{i} = strcat(var3{i},'H');
else
var3{i} = strcat(var3{i},'T');
end
end
end
% find outcomes with two heads
var4 = var3(var2 == 2);
display(var4);
var1 =
'000'
'001'
'010'
'011'
'100'
'101'
'110'
'111'
var2 =
3
2
2
1
2
1
1
0
var3 =
'HHH'
'HHT'
'HTH'
'HTT'
'THH'
'THT'
'TTH'
'TTT'
var4 =
'HHT'
'HTH'
'THH'

Plus de réponses (0)

Catégories

En savoir plus sur Spline Postprocessing dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by