我有N个时间步,想要创建与每个时间步对应的N个实数,如下所示:数字应该在[a,b]范围内,切换概率为p。 即,如果第k个数字(对应于第k个时间步长)是n,那么第k + 1个数字(对应于第k + 1个时间步长)是不同于n的任何其他数字的概率是p。所有创建的数字应该在[a,b]范围内。
如何在matlab中完成? 感谢。
答案 0 :(得分:0)
我不确定我是否满足您的所有要求,但这可能是您要搜索的脚本:
N = 10; % count of numbers
p = 0.2; % switching probability
a = 5;
b = 20;
% init empty numbers and get the first random number
numbers = zeros(N,1);
numbers(1) = rand(1) * (b-a) + a;
for iNumber = 2:N
% test if number must change
if rand(1) < (1-p)
% the number must be the same than the previous
% copy the value and go to next number
numbers(iNumber) = numbers(iNumber-1);
continue;
else
% a new number must be found
while 1
% get a new random number
numbers(iNumber) = rand(1) * (b-a) + a;
% check if the new random number is different from the previous
if numbers(iNumber) ~= numbers(iNumber-1)
% in case they are different, the while 1 can be stopped
break;
end
end % while 1
end % if rand(1) < (1-p)
end % for iNumber = 2:N
答案 1 :(得分:0)
保证超级随机:
N = randi(100);
p = rand;
l1 = rand*randi(100);
l2 = rand*randi(100);
if l2 < l1
[l2 l1] = deal(l1,l2);
end
out = []
while isempty(out)
if rand>rand
n = 2:N
a = rand([N,1])*(l2-l1)+l1;
x = rand([N-1,1])<p;
n(x==0)=n(x==0)-1;
n = [1 n];
out = a(n);
end
end