【问题标题】:Getting user defined possible solutions from a matrix, matlab从矩阵中获取用户定义的可能解决方案,matlab
【发布时间】:2014-05-19 15:07:57
【问题描述】:

我有一个矩阵

My_big_matrix_=rand(21,4)
   My_big_matrix_=[ 
   1.0000    0.8147    0.0357    0.7655
    2.0000    0.9058    0.8491    0.7952
    3.0000    0.1270    0.9340    0.1869
    4.0000    0.9134    0.6787    0.4898
    5.0000    0.6324    0.7577    0.4456
    6.0000    0.0975    0.7431    0.6463
    7.0000    0.2785    0.3922    0.7094
    8.0000    0.5469    0.6555    0.7547
    9.0000    0.9575    0.1712    0.2760
   10.0000    0.9649    0.7060    0.6797
   11.0000    0.1576    0.0318    0.6551
   12.0000    0.9706    0.2769    0.1626
   13.0000    0.9572    0.0462    0.1190
   14.0000    0.4854    0.0971    0.4984
   15.0000    0.8003    0.8235    0.9597
   16.0000    0.1419    0.6948    0.3404
   17.0000    0.4218    0.3171    0.5853
   18.0000    0.9157    0.9502    0.2238
   19.0000    0.7922    0.0344    0.7513
   20.0000    0.9595    0.4387    0.2551
   21.0000    0.6557    0.3816    0.5060];

我想获得不同的行组合以获得大小为 (7*4) 的矩阵

这些组合应该是

Combination_(1,1)={1,4,7,10,13,16,19}
Combination_(1,2)={1,5,7,10,13,16,19}
Combination_(1,3)={1,6,7,10,13,16,19}
Combination_(1,4)={2,4,7,10,13,16,19}
Combination_(1,5)={2,5,7,10,13,16,19}
Combination_(1,6)={2,6,7,10,13,16,19}
Combination_(1,7)={3,4,7,10,13,16,19}
Combination_(1,8)={3,5,7,10,13,16,19}
Combination_(1,9)={3,6,7,10,13,16,19}
Combination_(1,10)={1,4,7,10,13,16,19}
Combination_(1,11)={1,5,8,10,13,16,19}
Combination_(1,12)={1,6,9,10,13,16,19}

编辑: 在我的组合中,三行 (1,2,3),(4,5,6),(7,8,9),...(19,20,21)... 是组。只会选一名成员,绝不会选两名成员。所以可能的解决方案不会太高。

My_small_matrix_ having size of (7,4)

有什么想法吗?

【问题讨论】:

标签: matlab math matrix indexing combinations


【解决方案1】:

尝试使用以下代码将数据放入单元格数组中,其中每个单元格包含来自每个组合的数据 -

%// Input
My_big_matrix_ = rand(21,4)

%// Use allcomb to generate all such combinations. This a MATLAB
%// file-exchange code, available at -
%// http://www.mathworks.in/matlabcentral/fileexchange/10064-allcomb
allcomb_out = allcomb(1:3,4:6,7:9,10:12,13:15,16:18,19:21);

t1 = reshape(allcomb_out',[],1);%//'
t2 = My_big_matrix_(t1,:);

out = mat2cell(t2,7*ones(1,size(allcomb_out,1)),4); %// Desired output

【讨论】:

    【解决方案2】:
    C = combnk(1:21,7);   % C rows contains combinations
    numcomb = size(C, 1);
    combined = zeros(numcomb, 7, 3);
    for k = 1:numcomb
        combined(numcomb, :, :) = reshape(My_big_matrix(numcomb(k), :), 1, 7, 3);
    end
    

    【讨论】:

    • 好的,编辑状态你不想要所有行的组合。您列出的组合是全部还是只是您想要获得的示例?
    【解决方案3】:
    My_big_matrix_ = rand(21,4); %// data
    n = size(My_big_matrix_,1);
    g = 3; %// size of a group. Assumed to divide n
    
    m = n/g;
    combs = dec2base(0:g^m-1, g)-'0'+1;
    combs = bsxfun(@plus, combs, 0:g:g*(m-1));
    

    这给了

    combs =
         1     4     7    10    13    16    19
         1     4     7    10    13    16    20
         1     4     7    10    13    16    21
         1     4     7    10    13    17    19
         1     4     7    10    13    17    20
        [...]
         3     6     9    12    15    18    20
         3     6     9    12    15    18    21
    

    那么你的小矩阵是My_big_matrix_(combs(1,:),:)My_big_matrix_(combs(2,:),:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多