2014-11-04 61 views
0
T=5; 
p=0.3; 
A=[randsample(T,1,'true'); zeros(T-1,1)]; %the first element of A is uniformly 
              %randomly drawn from {1,...,T} 

我不知道該怎麼寫這段代碼在Matlab:伯努利過程在Matlab

%for t=2:T 
    %with probability p A(t)is A(t-1)+1 
    %with probability 1-p A(t) is uniformly randomly drawn from {1,...,T} 
%end 
+0

首先是什麼問題?你有錯誤嗎?如果是,什麼是堆棧跟蹤?你能把這些信息放進去嗎? – ha9u63ar 2014-11-04 09:07:14

回答

1

您可以使用rand獲得均勻分佈的隨機變量,randi獲得隨機整數。查閱文檔以瞭解它們的工作方式。

此代碼片段應該會對您有所幫助,它會在您的for循環內進行。我已經使用rand()<p以概率p發生某些事情,因爲從0到1之間的均勻分佈的隨機數的概率爲p小於p

if rand()<p 
    % this happens with probability p 
    A(t)=A(t-1)+1 
else 
    % this happens with probability 1-p 
    A(t)=randi(T) 
end