All possible combinations of a number of an array
Show older comments
Hi ,
I've a vector , a =[ 1 2 3]. I want to have all possible combinations of elements of a as belows ; 27 in total
o/p : a_o = [ 1 1 1 ; 1 1 2; 1 1 3; 1 2 1; 1 2 2 ; 1 2 3; 1 3 1 ; 1 3 2 ; 1 3 3 ; .........]
Please help me with its code.
Thanks in advance!
Accepted Answer
More Answers (1)
KSSV
on 27 Nov 2020
v = 1:3 ;
iwant = npermute(v,3)
2 Comments
Swati Sarangi
on 27 Nov 2020
You have to download the function from the link KSSV has provided.
But this submission produces only permutations without repititions, while the OP asks for e.g. [1 1 1] also. Then this will work: https://www.mathworks.com/matlabcentral/fileexchange/24325-combinator-combinations-and-permutations
index = combinator(3, 3, 'p', 'r');
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!