MATLAB:枚举任意数量的集合中的所有项目组合 [英] MATLAB: Enumerating All Combinations of Items in An Arbitrary Number of Sets

查看:444
本文介绍了MATLAB:枚举任意数量的集合中的所有项目组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
Matlab-生成以下元素的所有可能组合一些向量

Possible Duplicate:
Matlab - Generate all possible combinations of the elements of some vectors

说我有三套:

A = [5 6 7]
B = [0 1]
C = [11 22 33]

我想创建一个MATLAB函数,该函数可以采用任意数量的此类集合并吐出所有它们的组合.在上面的示例中,它将沿

I would like to create a MATLAB function that can take an arbitrary number of such sets and spit out all of their combinations. In the example above, it would spit out something along the lines of

[5 0 11
 5 0 22
 5 0 33
 5 1 11
 5 1 22 
 5 1 33
 ... 
 7 1 33]

我想到做这样的事情的唯一方法是使用嵌套的for循环作为跟随对象:

The only way that I can think about doing something like this is by using nested for loops as folows:

output = zeros(length(A)*length(B)*length(C), 3)
row = 1

for i = 1:length(A)
     for j = 1:length(B)
         for k = 1:length(C)

         output(row,:) = [A(i) B(j) C(k)];
         row = row + 1;

         end
      end
end

当然,如果不事先指定套数就无法使用-因此,我想知道是否存在解决此问题的简单方法或其他更聪明的方法?

Of course, this does not work without specifying the number of sets beforehand - so I'm wondering whether there is a simple fix or another smarter way around this problem?

推荐答案

尝试通过MATLAB文件交换进行allcomb

Try allcomb from the MATLAB file exchange

allcomb

这篇关于MATLAB:枚举任意数量的集合中的所有项目组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆