简体   繁体   English

如何在 MATLAB 中为矩阵的列向量生成所有可能的组合?

[英]How to generate all possible combinations for the column vectors of a matrix, in MATLAB?

If B=[1; 2]如果B=[1; 2] B=[1; 2] and A=[BB B...(n times B)] , how to obtain the matrix C corresponding to all the possible combinations between the column vectors of A .ie I want to get the combinations between n copies of the same vector. B=[1; 2]A=[BB B...(n times B)] ,如何获得矩阵C对应于A的列向量之间的所有可能组合。即我想得到相同的 n 个副本之间的组合向量。

For example, for n=3:例如,对于 n=3:

A =
     1     1     1
     2     2     2

So, C can be obtained using the function from File Exchange 'allcomb(varargin)':因此,可以使用来自文件交换“ C (varargin)”的 function 获得 C:

C=allcomb(A(:,1),A(:,2),A(:,3))
C =
     1     1     1
     1     1     2
     1     2     1
     1     2     2
     2     1     1
     2     1     2
     2     2     1
     2     2     2

In my case n is variable.在我的情况下,n 是可变的。 How to obtain C for any value of n?对于任何 n 值,如何获得C

You can put the repetitions in a cell, and use the {:} syntax to put all cell elements as inputs to allcomb您可以将重复项放在一个单元格中,并使用{:}语法将所有单元格元素作为allcomb的输入

n = 3;
B = [1,2];
A = repmat( {B}, n, 1 );
C = allcomb( A{:} ); % allcomb  is FileExchange.
                     % combvec  is a documented alternative.

Output: Output:

C =
 1     1     1
 1     1     2
 1     2     1
 1     2     2
 2     1     1
 2     1     2
 2     2     1
 2     2     2

Since the alphabets for each of the places is the same, this is really a base conversion.由于每个地方的字母表都相同,因此这实际上是基本转换。 MATLAB only accepts integer bases, but we can use that integer as an index into the alphabet B : MATLAB 只接受 integer 碱基,但我们可以使用 integer 作为字母表B的索引:

B=[1; 2];
n = 3;
b = numel(B);

for k = 0:(b^n-1)   % loop over all possible combinations
   C(k+1,:) = dec2base(k, b, n);
end
C = C - '0' + 1;   % convert 0..b-1 (in chars) into 1..b (in ints) for indexing
C = B(C);          % index into alphabet B

Results:结果:

>> C
C =

   1   1   1
   1   1   2
   1   2   1
   1   2   2
   2   1   1
   2   1   2
   2   2   1
   2   2   2

The last line of the script doesn't appear to do much in this case because the alphabet happens to be the same range as our indices, but changing the alphabet to B = [7; 14]在这种情况下,脚本的最后一行似乎没有太大作用,因为字母表恰好与我们的索引范围相同,但是将字母表更改为B = [7; 14] B = [7; 14] will correctly result in: B = [7; 14]将正确地导致:

C =

    7    7    7
    7    7   14
    7   14    7
    7   14   14
   14    7    7
   14    7   14
   14   14    7
   14   14   14

Funnily enough, allcomb from MATLAB File Exchange seems to be what you want.有趣的是,来自MATLAB文件交换的 allcomb 似乎正是您想要的。

allcomb([1; 2],[1; 2], [1; 2])

ans =

     1     1     1
     1     1     2
     1     2     1
     1     2     2
     2     1     1
     2     1     2
     2     2     1
     2     2     2

To do it for any n , simply construct the matrix with:要对任何n执行此操作,只需使用以下命令构造矩阵:

>> n = 3;
>> repmat(B, 1, n)

ans =

     1     1     1
     2     2     2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM