poster
2019-12-10, 20:48
我想生成给定数量的向量的元素的所有可能组合。
例如,对于[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中帮助我吗?是否已经有可以执行此任务的预定义功能?
回答:
在FileExchange (http://www.mathworks.com/matlabcentral/fileexchange/10064)尝试ALLCOMB (http://www.mathworks.com/matlabcentral/fileexchange/10064)函数。
如果将向量存储在单元格数组中,则可以这样运行它:
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
更多&回答... (https://stackoverflow.com/questions/4165859)
例如,对于[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中帮助我吗?是否已经有可以执行此任务的预定义功能?
回答:
在FileExchange (http://www.mathworks.com/matlabcentral/fileexchange/10064)尝试ALLCOMB (http://www.mathworks.com/matlabcentral/fileexchange/10064)函数。
如果将向量存储在单元格数组中,则可以这样运行它:
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
更多&回答... (https://stackoverflow.com/questions/4165859)