挣扎着几行编码。
这是上下文: 考虑一个公平的硬币被扔1000次并让X成为的实验 三个连续头部的模式出现的次数。注意 允许重叠,因此如果序列是HHHHTT···TT,那么X = 2。 估计 X的概率质量函数并用它来估计 X的均值和方差。报告您的(时刻)估计值。
这是提供的模板,并突出显示了我需要添加的5行代码及其说明。
clear all
N=1000; %Number of Die Rolls
nreps=10000;
count=zeros(1,N+1); %count will be a vector of size N+1 such that, for i=0...N, c(i+1)/nreps=P(X=i)
for n=1:nreps
n_3H = 0; %count the number of occurrences of the pattern HHH
***%Add code to toss a coin N times***
for i=3:N
if ***%Add condition that the pattern HHH appears at position (i-2,i-1,i)***
n_3H = n_3H+1;
end
end
***%Add code to increment count(n_3H+1) by 1***
end
RelFreq = count/nreps;
meanX ***%Add code to estimate the mean of X***
varX ***%Add code to estimate the variance of X***
到目前为止,这就是我所拥有的,但它似乎没有返回任何东西,我不确定我应该做些什么来继续。
clear all
N=1000; %Number of Die Rolls
nreps=100;
count=zeros(1,N+1);%count will be a vector of size N+1 such that, for i=0...N, c(i+1)/nreps=P(X=i)
for n=1:nreps
n_3H = 0; %count the number of occurrences of the pattern HHH
flipStates = randi([1, 2], 1, N) %Add code to toss a coin N times
for i=3:N
if strfind(flipStates, [1,1,1])%Add condition that the pattern HHH appears at position (i-2,i-1,i)
n_3H = n_3H+1;
end
end
%Add code to increment count(n_3H+1) by 1
end
RelFreq = count/nreps;
meanX %Add code to estimate the mean of X
varX %Add code to estimate the variance of X
任何帮助将不胜感激!
答案 0 :(得分:0)
因为在整个抛掷序列中循环并分别检查每个子序列,你应该这样做:
if all(flipStates(i-2:i) == [1,1,1])
你所做的是检查MATLAB翻译为if
的向量的if all(vector)
条件。
如果你把它取出(实际上是代替)循环,你的方法是正确的:for i=3:N ...
并执行:n_3H = numel(strfind(flipStates,[1 1 1]))