0
用幾行代碼來掙扎。找到正確的代碼行來確定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
任何幫助,將不勝感激!
你知道我需要使用查找均值和X的變化什麼行代碼?我的n_3H是否顯示了HHH出現在我第一次重複的次數或所有100次重複的平均次數?不過謝謝你,我嘗試了你最初的建議,而現在n_3H實際上給出了一個有效的答案! –
@PaulDresner檢查函數['mean'](https://es.mathworks.com/help/matlab/ref/mean.html)和['var'](https://es.mathworks.com/help /matlab/ref/var.html) –
我需要寫下什麼代碼才能讓我在我的n_3H中有多個答案,這將允許我計算平均值? @LuisMendo –