How to obtain all possible combinations of cell array?

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)

See if the nchoosek (link) or perms (link) functions will do what you want. One of them should work for you.

6 Commenti

Sadly, they don't! nchoosek does not take into account the order, therefore, I only get 9 combinations when I'm supposed to have 20 (and that's only with two elements..); and perms does not take into account with how many elements from the array I need the combinations because it permutes the whole array .
:(
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
Yes that's exactly it! However, it doesn't work when I need three elements..
combos3 =
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
But even if I flip it, it still doesn't give all possible orders!
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
Ok, I'm not explaining myself well here :) I need these combinations:
Combos2=
1 2
1 3
1 4
1 5
2 1
2 3
2 4
2 5
3 1
3 2
3 4
3 5
4 1
4 2
4 3
4 5
5 1
5 2
5 3
5 4
And for the three elements I need all these combinations:
Combos3=
123
124
125
132
134
135
142
143
145
152
153
154
213
214
215
231
234
235
241
243
245
312
314
315
321
324
325
341
342
345
351
352
354
412
413
415
421
423
425
431
432
435
451
452
453
512
513
514
521
523
524
531
532
534
541
542
543
There! I really hope you can help me.
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

Accedi per commentare.

Categorie

Richiesto:

il 9 Apr 2017

Commentato:

il 9 Apr 2017

Community Treasure Hunt

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

Start Hunting!

Translated by