user3210485
user3210485

Reputation: 5

selecting an element randomly from the array all combination

I randomly selected elements of an array by matlab code. I selected 8 elements from array(1,64) . Now, I want to create all combination . Example array=[1 2 3 4 ... 64] I randmly selected 8 elements new=[1 2 3 4 5 6 7 8 ] new=[1 2 3 4 5 6 7 9 ]... new is all combination. How can I create all combination? for matlab code

Upvotes: 0

Views: 171

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112679

Assuming order is not important (that is, [1 2 3 4 5 6 7 8] don't count as different combinations [1 2 3 4 5 6 8 7]), use

combinations = nchoosek(1:64,8);

But it may take much time and RAM.

For example,

>> combinations = nchoosek(1:5,3)

gives

combinations =

     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

To randomly generate just one of those combinations, without having to compute them all previously:

combination = sort(randsample(1:64,8));

Upvotes: 2

Related Questions