How to obtain all possible combinations of cell array?

4 views (last 30 days)
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!

Answers (1)

Star Strider
Star Strider on 9 Apr 2017
See if the nchoosek (link) or perms (link) functions will do what you want. One of them should work for you.
  6 Comments
Dada
Dada on 9 Apr 2017
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.
Star Strider
Star Strider on 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

Sign in to comment.

Categories

Find more on Discrete Math 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!