MATLAB Answers

all possible combinations of three vectors

90 views (last 30 days)
Rogier Busscher
Rogier Busscher on 24 May 2017
Commented: Bruno Luong on 15 Oct 2020
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?

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 24 May 2017
One possible way, which doesn't require any toolbox:
[ca, cb, cc] = ndgrid(A, B, C);
combs = [ca(:), cb(:), cc(:)]

  3 Comments

Rogier Busscher
Rogier Busscher on 24 May 2017
It seems to be working, thank you dearly!
Devsmit Ranparia
Devsmit Ranparia on 15 Oct 2020
@guillaume is it possible use ndgrid when I don't know explicilty the number of output arguements. Based on some number 'n' my code must be able to keep output arguements as [x1,x2,...,xn] = ndgrid(X).
Thanks in advance!

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 24 May 2017
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(:)];

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!