How can I obtain all possible combinations of given vectors in MATLAB?

285 ビュー (過去 30 日間)
I want to obtain all possible combinations of a set of vectors. For example, if
a=1:3; b=4:5
I want to generate the following vector:
C=[1 4;...
1 5;...
2 4;...
2 5;...
3 4;...
3 5]

採用された回答

MathWorks Support Team
MathWorks Support Team 2023 年 4 月 14 日
編集済み: MathWorks Support Team 2023 年 4 月 19 日
There are several ways to obtain all possible combinations of a set of vectors.
a) If the set consists of 2 vectors, a and b, you can execute the following code:
[A,B] = meshgrid(a,b);
c=cat(2,A',B');
d=reshape(c,[],2);
b) If the set consists of 2 or more vectors, you can use the Neural Network Toolbox function COMBVEC to achieve the desired result. More information about COMBVEC function can be obtained form the following link:
c) If Neural Network Toolbox is not available, you can achieve the desired result from MATLAB Central file exchange* function through the following link:
*Note that MathWorks does not guarantee or warrant the use or content of these submissions. Any questions, issues, or complaints should be directed to the contributing author.
  2 件のコメント
Adam Danz
Adam Danz 2018 年 7 月 25 日
+1 To add to this...
to avoid repeat combinations in the form of [20 30; 30 20]
d = unique(sort(d,2), 'rows')
And to remove self-combinations such as [20 20]
d(d(:,1)==d(:,2),:) = []
Adam Danz
Adam Danz 2021 年 12 月 7 日
編集済み: Adam Danz 2021 年 12 月 7 日
Note that ndgrid provides more compatible results with combvec, rather than using meshgrid.
a = 1:3;
b = 5:6;
c= 12:14;
M1 = combvec(a,b,c)
M1 = 3×18
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 5 5 5 6 6 6 5 5 5 6 6 6 5 5 5 6 6 6 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14
[an, bn, cn] = ndgrid(a,b,c);
M2 = [an(:), bn(:), cn(:)]'
M2 = 3×18
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 5 5 5 6 6 6 5 5 5 6 6 6 5 5 5 6 6 6 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14
isequal(M1,M2) % same as combvec
ans = logical
1
[am, bm, cm] = meshgrid(a,b,c);
M3 = [am(:), bm(:), cm(:)]'
M3 = 3×18
1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 5 6 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14
isequal(M1,M3) % requires reshaping
ans = logical
0

サインインしてコメントする。

その他の回答 (2 件)

stewpend0us
stewpend0us 2017 年 1 月 30 日
This worked for me (probably the same thing that's going on in the "ALLCOMB" function that was suggested):
elements = {1:2, 3:5, 6:7, 8:10}; %cell array with N vectors to combine
combinations = cell(1, numel(elements)); %set up the varargout result
[combinations{:}] = ndgrid(elements{:});
combinations = cellfun(@(x) x(:), combinations,'uniformoutput',false); %there may be a better way to do this
result = [combinations{:}]; % NumberOfCombinations by N matrix. Each row is unique.
  1 件のコメント
Rik
Rik 2017 年 7 月 19 日
Thanks, it took quite some effort to find this answer. I'm glad I don't have to hack something with a decision tree or n nested loops...

サインインしてコメントする。


Steven Lord
Steven Lord 2023 年 3 月 16 日
As of release R2023a you can use the combinations function in MATLAB for this task.
T = combinations(1:3, 4:6)
T = 9×2 table
Var1 Var2 ____ ____ 1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6
While combinations does return a table, if the data in all of the inputs are compatibly typed you can extract the contents of the table as a matrix in one line.
M = combinations(1:3, 4:6).Variables
M = 9×2
1 4 1 5 1 6 2 4 2 5 2 6 3 4 3 5 3 6
The table output allows for mixing of types.
T = combinations(1:3, ["red", "blue", "green"])
T = 9×2 table
Var1 Var2 ____ _______ 1 "red" 1 "blue" 1 "green" 2 "red" 2 "blue" 2 "green" 3 "red" 3 "blue" 3 "green"

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2007b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by