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 回表示 (過去 30 日間)
please keep in mind 1 and 2 is different combination and 2 and 1 is different combination.

採用された回答

Guillaume
Guillaume 2018 年 8 月 7 日
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 件のコメント
Guillaume
Guillaume 2018 年 8 月 7 日
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
Sun Heat
Sun Heat 2018 年 8 月 8 日
this is universal code for n number of combination.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by