How can I print the outcomes of a binomial distribution experiment in Matlab ?
2 次查看(过去 30 天)
显示 更早的评论
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:
采纳的回答
Di Zhu
2016-5-20
编辑:Di Zhu
2016-5-20
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'
0 个评论
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!