How to obtain all possible combinations of cell array?
Mostra commenti meno recenti
I have a cell array {'One','Two','Three','Four','Five'} I need to obtain all possible combinations with 2 and with 3 elements from this array. Like this:
One Two
One Three
...
Three one
Three Two
...
Four One
Four Two ...
And so on, mixing also the order. I need it with two elements and with three of this cell array, how is it posible to do so?
thank you!
Risposte (1)
Star Strider
il 9 Apr 2017
1 voto
6 Commenti
Dada
il 9 Apr 2017
Star Strider
il 9 Apr 2017
I’m not certain what you want.
See if this works:
v = nchoosek(1:5,2);
combs = [v; fliplr(v)]
combs =
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
2 1
3 1
4 1
5 1
3 2
4 2
5 2
4 3
5 3
5 4
Dada
il 9 Apr 2017
Star Strider
il 9 Apr 2017
I’m still not sure what you’re going for.
Try this:
v3 = nchoosek(1:5, 3);
combs3 = [v3; circshift(v3,1,2); circshift(v3,2,2)]
combs3 =
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5
3 1 2
4 1 2
5 1 2
4 1 3
5 1 3
5 1 4
4 2 3
5 2 3
5 2 4
5 3 4
2 3 1
2 4 1
2 5 1
3 4 1
3 5 1
4 5 1
3 4 2
3 5 2
4 5 2
4 5 3
Dada
il 9 Apr 2017
Star Strider
il 9 Apr 2017
I decided to just ‘brute force’ it:
for k1 = 1:500
combs3(k1,:) = randperm(5,3);
end
combs3 = unique(combs3, 'rows')
combs3 =
1 2 3
1 2 4
1 2 5
1 3 2
1 3 4
1 3 5
1 4 2
1 4 3
1 4 5
1 5 2
1 5 3
1 5 4
2 1 3
2 1 4
2 1 5
2 3 1
2 3 4
2 3 5
2 4 1
2 4 3
2 4 5
2 5 1
2 5 3
2 5 4
3 1 2
3 1 4
3 1 5
3 2 1
3 2 4
3 2 5
3 4 1
3 4 2
3 4 5
3 5 1
3 5 2
3 5 4
4 1 2
4 1 3
4 1 5
4 2 1
4 2 3
4 2 5
4 3 1
4 3 2
4 3 5
4 5 1
4 5 2
4 5 3
5 1 2
5 1 3
5 1 4
5 2 1
5 2 3
5 2 4
5 3 1
5 3 2
5 3 4
5 4 1
5 4 2
5 4 3
Categorie
Scopri di più su Resizing and Reshaping Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!