MATLAB 中带有权重的数字随机排列

Random permutation of numbers in MATLAB with weights

如何随机化向量 a 中的数字,并以我可以控制哪些数字 'follow' 其他数字的方式分配权重?

假设:

a = [ 1 2 3 4]

我想获得这样的东西:

1 2 1 3 4 2 1 4 3 4 1 3 4 1 ....

我的目标是创建一个更长的向量(包括从 1 到 4 的这些数字,排列),同时添加权重:

我设法通过排列这些数字获得了一个更长的向量,但我不知道如何应用这些 weights/rules。

使用马尔可夫链的这个转移矩阵:

M = 1/9*[2, 3, 2, 2; ...
         3, 2, 2, 2; ...
         2, 2, 2, 3; ...
         3, 2, 2, 2];

使用以下算法:

function realizations = realizeMarkovChain(M, start, numSteps)
%// Generates realization of Markov chain given by transition matrix M.
%// The probabilities of going from state i to a different state are given
%// by the rows M(i,:) of the transition matrix
currentPosition = start;
cdf = cumsum(M,2);
realizations = zeros(1, numSteps);

for i = 1:numSteps
   currentPosition = find(rand < cdf(currentPosition,:), 1, 'first');
   realizations(i) = currentPosition;
end