# all possible combinations of three vectors

조회 수: 46 (최근 30일)
Rogier Busscher 2017년 5월 24일
댓글: Bruno Luong 2021년 4월 22일
I am trying to find all possible combinations of three vectors (A,B,C). For this, I tried using combvec. However, the result gets me a 1281x1 double, while i expected a matrix of 546*33*649 possibilities.
What i would like to get is:
A: 1,5,6,9,12
B: 1,2,3,4,5,6
C: 3,18,27,69,72
Ans:
1,1,3
1,1,18
1,1,27
etc.
So how do i do this?

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

### 채택된 답변

Guillaume 2017년 5월 24일
One possible way, which doesn't require any toolbox:
[ca, cb, cc] = ndgrid(A, B, C);
combs = [ca(:), cb(:), cc(:)]
##### 댓글 수: 5이전 댓글 3개 표시이전 댓글 3개 숨기기
Abdelmajid Ben yahya 2021년 4월 22일
Hi,
I have an additional question, if i may.
Is it possible to get all combinations that fulfil a certain constraint? Let's say that within the combination A+B+C<=4.
Thank you.
Bruno Luong 2021년 4월 22일
Just filter out those are not meet the constraints, or you can use this FEX to get directly the combination
allVL1(3,4,'<=')
ans =
0 0 0
0 0 1
0 1 0
1 0 0
0 0 2
0 1 1
0 2 0
1 0 1
1 1 0
2 0 0
0 0 3
0 1 2
0 2 1
0 3 0
1 0 2
1 1 1
1 2 0
2 0 1
2 1 0
3 0 0
0 0 4
0 1 3
0 2 2
0 3 1
0 4 0
1 0 3
1 1 2
1 2 1
1 3 0
2 0 2
2 1 1
2 2 0
3 0 1
3 1 0
4 0 0

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

### 추가 답변 (1개)

Azzi Abdelmalek 2017년 5월 24일
A=[1,5,6,9,12]
B= [1,2,3,4,5,6]
C= [3,18,27,69,72]
[ii,jj,kk]=meshgrid(A,B,C);
ii=permute(ii,[1 3 2]);
jj=permute(jj,[2 1 3])
kk=permute(kk,[3 2 1])
out=[ii(:) jj(:) kk(:)];

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

### 카테고리

Help CenterFile Exchange에서 Sequence and Numeric Feature Data Workflows에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by