在matlab中将数字列表分为3组

时间:2014-07-28 17:42:31

标签: matlab

我有一个数字列表,[1:9],我需要划分三组。每个组必须至少包含一个数字。我需要枚举所有组合(即顺序无关紧要)。理想情况下,输出是x乘3阵列。有关如何在matlab中执行此操作的任何想法?

3 个答案:

答案 0 :(得分:0)

这就是你想要的:

x = 1:9;
n = length(x);
T=3;

out = {};
%// Loop over all possible solutions
for k=1:T^n
    s = dec2base(k, T, n);
    out{k}{T} = [];
    for p=1:n
        grpIndex = str2num(s(p))+1;
        out{k}{grpIndex} = [out{k}{grpIndex} x(p)];
    end
end

%// Print result. size of out is the number of ways to divide the input. out{k} contains 3 arrays with the values of x
out

答案 1 :(得分:0)

也许这就是你想要的。我假设分组中的分裂是单调的#34;,也就是说,首先是第一组的元素,然后是第二组的元素。

n = 9; %// how many numbers
k = 3; %// how many groups
b = nchoosek(1:n-1,k-1).'; %'// "breaking" points
c = diff([ zeros(1,size(b,2)); b; n*ones(1,size(b,2)) ]); %// result

c的每列都会显示k群组的尺寸

c =

  Columns 1 through 23

     1     1     1     1     1     1     1     2     2     2     2     2     2     3     3     3     3     3     4     4     4     4     5
     1     2     3     4     5     6     7     1     2     3     4     5     6     1     2     3     4     5     1     2     3     4     1
     7     6     5     4     3     2     1     6     5     4     3     2     1     5     4     3     2     1     4     3     2     1     3

  Columns 24 through 28

     5     5     6     6     7
     2     3     1     2     1
     2     1     2     1     1

答案 2 :(得分:0)

这产生了我想要的东西。函数nchoosekr_rec()也如下所示。

for x=1:7
    numgroups(x)=x;
end

c=nchoosekr_rec(numgroups,modules);
i=1;
d=zeros(1,modules);
for x=1:length(c(:,1))
    c(x,modules+1)=sum(c(x,1:modules));
    if c(x,modules+1)==length(opt_mods)
        d(i,:)=c(x,1:modules);
        i=i+1;
    end
end

numgroups=[];
for x=1:length(opt_mods)
    numgroups(x)=x;
end


count=0;

for x=1:length(d(:,1))
    combos=combnk(numgroups,d(x,1));
    for y=1:length(combos(:,1))
        for z=1:nchoosek(9-d(x,1),d(x,2))
            new_mods{count+z,1}=combos(y,:);
            numgroups_temp{count+z,1}=setdiff(numgroups,new_mods{count+z,1});
        end
        count=count+nchoosek(9-d(x,1),d(x,2));
    end
end

count=0;

for x=1:length(d(:,1))

    for y=1:nchoosek(9,d(x,1))
        combos=combnk(numgroups_temp{count+1},d(x,2));
        for z=1:length(combos(:,1))
            new_mods{count+z,2}=combos(z,:);
            new_mods{count+z,3}=setdiff(numgroups_temp{count+z,1},new_mods{count+z,2});
        end
        count=count+length(combos(:,1));
    end
end





  function y = nchoosekr_rec(v, n)

   if n == 1
      y = v;
   else
      v = v(:);
      y = [];
      m = length(v);
      if m == 1
         y = zeros(1, n);
         y(:) = v;
      else
         for i = 1 : m
         y_recr = nchoosekr_rec(v(i:end), n-1);
         s_repl = zeros(size(y_recr, 1), 1);
         s_repl(:) = v(i);
         y = [ y ; s_repl, y_recr ];
         end
      end
   end