0

combnk用来生成组合列表。但是,结果形状不是我需要的数据。我想要例如combnk(1:3,2)

1 1 0  
0 1 1
1 0 1

不是

1 2
1 3
2 3

我该怎么做?我怎样才能改变combnk以最佳方式给出结果?

4

2 回答 2

0

另一种解决方案:

c = combnk(1:3,2);
r = repmat(1:size(c,1), [1 size(c,2)]);
output = full(sparse(r,c(:),1))

结果:

output =
     1     1     0
     1     0     1
     0     1     1
于 2012-07-04T14:45:44.333 回答
0

你不是说你想要

1 1 0 
1 0 1 
0 1 1

代替

1 2
1 3
2 3

所以每一行都是原始向量的逻辑选择向量v

您可以通过以下方式获得此信息:

v = 1:3;
k = 2;

tmp = combnk(v,k);
M = size(tmp,1);

output = false(M,numel(v));
output(sub2ind(size(output),repmat((1:M)',1,k),tmp))=true;

结果:

output =

     1     1     0
     1     0     1
     0     1     1
于 2012-07-04T14:18:45.123 回答