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

2 views (last 30 days)
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:

Accepted Answer

Di Zhu
Di Zhu on 20 May 2016
Edited: Di Zhu on 20 May 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'

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!