生成某些向量的元素的所有可能组合(笛卡尔积)

编程入门 行业动态 更新时间:2024-10-10 07:21:41
本文介绍了生成某些向量的元素的所有可能组合(笛卡尔积)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想生成给定数量的向量的元素的所有可能组合.

例如,对于[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个,我需要 generalization .您能在MATLAB中帮助我吗?已经有可以执行此任务的预定义功能吗?

解决方案

在FileExchange尝试 ALLCOMB 函数. /p>

如果将向量存储在单元格数组中,则可以这样运行它:

a = {[1 2], [1 2], [4 5]}; allcomb(a{:}) ans = 1 1 4 1 1 5 1 2 4 1 2 5 2 1 4 2 1 5 2 2 4 2 2 5

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?

解决方案

Try ALLCOMB function at FileExchange.

If you store you vectors in a cell array, you can run it like this:

a = {[1 2], [1 2], [4 5]}; allcomb(a{:}) ans = 1 1 4 1 1 5 1 2 4 1 2 5 2 1 4 2 1 5 2 2 4 2 2 5

更多推荐

生成某些向量的元素的所有可能组合(笛卡尔积)

本文发布于:2023-11-28 21:39:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:笛卡尔   组合   向量   元素

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!