在 MATLAB 中查找正确的代码行以确定分布时遇到麻烦



为几行编码而苦苦挣扎。

这是上下文:考虑一个实验,一个公平的硬币被抛掷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

任何帮助将不胜感激!

由于在整个

投掷序列中循环并单独检查每个子序列,您应该执行以下操作:

if all(flipStates(i-2:i) == [1,1,1])

您所做的是检查 MATLAB 转换为 if all(vector) 的向量上的if条件。

如果你把它拿出来(实际上是代替(循环:for i=3:N ...和做:n_3H = numel(strfind(flipStates,[1 1 1])),你的方法是正确的

最新更新