MATLAB Answers

# i have 16 different types of files and i have to take the files in the bunch of 2 from the 16 files, how i put permutation and combination to get 2 files from the 16 files each time differently?

1 view (last 30 days)
RAVI on 7 Aug 2018
Commented: RAVI on 8 Aug 2018
please keep in mind 1 and 2 is different combination and 2 and 1 is different combination.

#### 0 Comments

Sign in to comment.

### Accepted Answer

Guillaume on 7 Aug 2018
Since there are only 240 combinations, it's trivial to generate them all as a 240x2 matrix:
allperms = nchoosek(1:16, 2);
allperms = [allperms; fliplr(allperms)];
You can then choose a row at random:
oneperm = allperms(randi(size(allperms, 1)), :)
or shuffle all the rows
allperms = allperms(randperm(size(allperms, 1)), :)

#### 4 Comments

Show 1 older comment
RAVI on 7 Aug 2018
i think it works.
Guillaume on 7 Aug 2018
No, It does not extend to more than two elements. A generic version:
v = 1:16;
n = 4;
allperms = nchoosek(v, n); %all combinations
colperms = num2cell(perms(1:n), 2); %how many ways can we swap the columns
allperms = cell2mat(cellfun(@(p) allperms(:, p), colperms, 'UniformOutput', false)) %swap the columns all possible ways
RAVI on 8 Aug 2018
this is universal code for n number of combination.

Sign in to comment.

Translated by