How to create matrix of all combinations?
39 ビュー (過去 30 日間)
古いコメントを表示
Suppose I have variables x1, x2, x3. x1 can be any element of the set {1,2,3}, x2 can be {1,2,3,4,5} and x3 can be {10,11,12,13,14,15} for example. How to create the matrix of all combinations, in which each row is a possible combination: [3,4,12] for example. Is it possible to do it without a for cycle with some tricks?
0 件のコメント
回答 (2 件)
Patrick Zwierzynski
2018 年 7 月 27 日
編集済み: Patrick Zwierzynski
2018 年 7 月 27 日
If you change x1, x2, and x3 to be vectors instead of sets, you can use the "combvec" function and simply call "combvec(x1, x2, x3)". You can then use the column indices of the resulting matrix to select the different combinations.
Bruno Luong
2018 年 7 月 27 日
c={[1 2 3],[1 2 3 4 5],[10 11 12 13 14 15]}
[c{:}]=ndgrid(c{:});
n=length(c);
c = reshape(cat(n+1,c{:}),[],n)
2 件のコメント
Matt J
2018 年 8 月 23 日
Not slower than it should be. If you have many combinations, then naturally it will take a long time.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!