Matlab如何随机分布矩阵元素

Matlab how to distribute a matrix elements randomly

大家好如何让一个矩阵随机分布到另一个矩阵n,

m = [ 1 1 3 3 3 4 4 6 6 7 7 7];
n = zeros(3,10);

序列中必须有相同的值,例如:4 4 4, 7 7 7.result reqiured 可以是类似{或其他组合):

distributed_matrix =

 0     1     1     0     7     7     7     0     0     0
 0     0     3     3     3     4     4     0     0     0
 6     6     6     0     0     0     0     0     0     0

谢谢...

如果我正确理解你的问题,可能的解决方案是

m = [ 1 1 3 3 3 4 4 6 6 7 7 7];
n = zeros(3,10);
p= randperm(numel(n)); % generate the random permutation
n(p(1:length(m)))= m   % assign the elments of m to the elements with indices taken 
                       % from the first length(m) numbers of random permutation

如果您不对 m 的元素分布顺序施加任何限制,那么 randsample 可能会有所帮助:

ridx = randsample( numel(n), numel(m) ); %// sample new idices for elelemtns
n(ridx) = m;

查看 ,事情变得有点混乱。
要识别 m 中的序列及其范围,您可以:

idx = [1 find(diff(m)~=0)+1]; 
extent = diff([idx numel(m)+1]);  %// length of each sequence
vals = m(idx);  %// value of each sequence

一旦你有了序列和它们的长度,你就可以随机打乱它们,然后沿着线分布...