【问题标题】:Matlab how to distribute a matrix elements randomlyMatlab如何随机分布矩阵元素
【发布时间】:2017-02-17 20:00:41
【问题描述】:

大家好,如何让一个矩阵随机分布到另一个矩阵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 reqired 可以是类似{或其他组合):

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

谢谢你...

【问题讨论】:

  • 您是否需要序列(1, 13 3 3 等)“保持在一起”?
  • 是的,他们一直在一起……
  • 可以在n 的行之间拆分相同的值吗?那是[... 7 7; 7 ...]?
  • 不,它不能...它必须在同一行...谢谢..
  • 如果m 的值大于size(n,2) 怎么办?它不适合一行...

标签: matlab matrix distribute


【解决方案1】:

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

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

查看additional constraints,事情变得有点混乱。
要识别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

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

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

    • 谢谢你的回答,但是如何让相同的值留在序列中呢?谢谢...
    猜你喜欢
    • 1970-01-01
    • 2014-09-19
    • 2014-10-22
    • 1970-01-01
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多