生成某些向量元素的所有可能组合(笛卡尔积)
我想生成给定数量的向量的元素的所有可能的组合。
例如,对于 [1 2]
、[1 2]
和 [4 5]
我想生成元素:
[ 1 1 4; 1 1 5; 1 2 4; 1 2 5; 2 1 4; 2 1 5; 2 2 4; 2 2 5]
问题是我不知道需要计算组合的向量数量。在本例中可能有 3 个,也可能有 10 个,我需要一个概括。你能在 MATLAB 中帮我解决这个问题吗?是否已经有一个预定义函数可以完成此任务?
I would like to generate all the possible combinations of the elements of a given number of vectors.
For example, for [1 2]
, [1 2]
and [4 5]
I want to generate the elements:
[1 1 4; 1 1 5; 1 2 4; 1 2 5; 2 1 4; 2 1 5; 2 2 4; 2 2 5]
The problem is that I don't know the number of vectors for which I need to calculate the combinations. There might be 3 as in this case, or there may be 10, and I need a generalization. Can you please help me to this in MATLAB? Is there already a predefined function that can do this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
考虑使用 NDGRID 函数解决此解决方案:
或者如果您想要通用解决方案对于任意数量的集合(无需手动创建变量),请使用此函数定义:
请注意,如果您愿意,可以对结果进行排序:
此外,上面的代码不会检查集合是否没有重复值(例如:
{[1 1] [1 2] [4 5]}
)。如果您想添加这一行:Consider this solution using the NDGRID function:
Or if you want a general solution for any number of sets (without having to create the variables manually), use this function definition:
Note that if you prefer, you can sort the results:
Also, the code above does not check if the sets have no duplicate values (ex:
{[1 1] [1 2] [4 5]}
). Add this one line if you want to:尝试 FileExchange 中的 ALLCOMB 函数。
如果将向量存储在元胞数组中,则可以像这样运行它:
Try ALLCOMB function at FileExchange.
If you store you vectors in a cell array, you can run it like this:
这个最新的答案提供了两个额外的解决方案,其中第二个是解决方案(在我看来)以及通过应用 MATLAB 强大的逗号分隔列表对 Amro 的答案解决方案与
ndgrid
的改进而不是高性能的元胞数组,正如 Amro 在他的回答中所做的那样,逗号分隔的列表语法 (
v{:}
) 提供了ndgrid
的输入和输出。不同之处(第四行)是,它通过应用逗号分隔列表来避免cellfun
和cell2mat
,现在再次作为cat
的输入:使用
cat
和reshape
将执行时间几乎减少一半。 我对另一个问题的回答和更正式的是路易斯·门多。This late answers provides two additional solutions, where the second is the solution (in my opinion) and an improvement on Amro's answer solution with
ndgrid
by applying MATLAB's powerful comma-separated lists instead of cell arrays for high performance,combvec
Just as Amro did in his answer, the comma-separated lists syntax (
v{:}
) supplies both the inputs and outputs ofndgrid
. The difference (fourth line) is that it avoidscellfun
andcell2mat
by applying comma-separated lists, again, now as the inputs tocat
:The use of
cat
andreshape
cuts execution time almost in half. This approach was demonstrated in my answer to an different question, and more formally by Luis Mendo.从 MATLAB 2023a 开始,您可以使用
combinations
函数,支持向量、矩阵、元胞数组及其混合:输出的形式为 table,支持混合类型。如果您的输出仅需要单一类型,您可以使用
table2array
:就其价值而言,我编写了一个填充程序来模拟旧版本 MATLAB(低至版本 2013b)中
组合
的行为,包括对混合的支持类型:Since MATLAB 2023a you can use the
combinations
function, which supports vectors, matrices, cell arrays, and mixtures thereof:The output is in the form of a table, which supports mixed types. If your outputs need only a single type, you can convert the output to a matrix using
table2array
:For what it's worth, I have written a shim that emulates the behaviour of
combinations
in older versions of MATLAB (down to version 2013b), including support for mixed types:我们还可以在 matlab 中使用“combvec”指令
希望它有帮助。
祝你好运。
we can also use the 'combvec' instruction in matlab
Hope it helps.
Good luck.