我有这个算法,但我不太喜欢许多 if 语句。
有人可以看看这个函数是否可以用更简洁的方式编写吗?
rand('twister',101)
n = 10;
f = A.^(0:n)./factorial(0:n);
f = f/sum(f);
n = 10000;
Xi = 2;
X = zeros(1,n);
for i =1:n,
dXi = (-1)^round(rand);
Yi = Xi + dXi;
if Yi > 0 & Yi <= length(f),
if f(Yi) >= f(Xi),
X(i) = Yi;
Xi = Yi;
else
if rand <= f(Yi)/f(Xi),
X(i) = Yi;
Xi = Yi;
else
X(i) = Xi;
end
end
end
X(i) = Xi;
end