我有一个关于处理偏置硬币的 MATLAB 问题的问题。假设我想模拟一个偏硬币,其出现正面的概率符合以下数据集p ={0.5,0.4,0.3,0.2, 0.1}
。
我如何从这个数据集中任意选择一个假设的硬币,并确定它在 MATLAB 中是否是无偏的,因为N是100到1000步长为100的硬币翻转N次。由于我对 Matlab 的了解有限,我希望能在这个项目中得到一些帮助。我发现这个网站有一些指针http://www.wikihow.com/Simulate-a-Fair-Coin-Toss-With-a-Biased-Coin
我的两次抛硬币的matlab代码
function side = simulateOneToss
% Make two tosses (outcomes 0 and 1 could stand for heads and tails)
twoTosses = round(rand(1,2));
% If outcome is not HT or TH (both of which sum to 1), try again.
while sum(twoTosses) ~= 1
twoTosses = round(rand(1,2));
end
% Take first of the two tosses as the answer.
side = twoTosses(1);
我的第一个问题代码
function outcome = mysim(p, N)
P = cumsum(p);
u = rand(1, N);
outcome = zeros(1, N); % A blank array for holding the outcomes `enter code here`
for n=100:100:N,
h = find(u(n)<P, 1 );
outcome(n) = h;
end