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中编写这段代码:
%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 :(得分: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