How to create matrix of all combinations?

조회 수: 45 (최근 30일)
Mr M.
Mr M. 2018년 7월 27일
댓글: Matt J 2018년 8월 23일
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?

답변 (2개)

Patrick Zwierzynski
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.
Here's the link to the documentation: https://www.mathworks.com/help/nnet/ref/combvec.html
  댓글 수: 1
Mr M.
Mr M. 2018년 7월 27일
Unfortunately I do not have the necessary toolbox for combvec

댓글을 달려면 로그인하십시오.


Bruno Luong
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
Mr M.
Mr M. 2018년 7월 27일
[c{:}]=ndgrid(c{:}); is very very very slow! :(
Matt J
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by