Generate all possible permutations including repeats

I am trying to generate all possible combinations of 0,1,2 including repeats.
I have tried:
F = [0,1,2];
perms(F)
That works but it only gives the combinations with no repeats:
2 1 0
2 0 1
1 2 0
1 0 2
0 2 1
0 1 2
Including repeats there should be 27 combinations instead of just 6 without repeats

 採用された回答

Bruno Luong
Bruno Luong 2023 年 10 月 25 日
編集済み: Bruno Luong 2023 年 10 月 25 日

0 投票

F = [0,1,2];
combinations(F,F,F)
ans = 27×3 table
F F_1 F_2 _ ___ ___ 0 0 0 0 0 1 0 0 2 0 1 0 0 1 1 0 1 2 0 2 0 0 2 1 0 2 2 1 0 0 1 0 1 1 0 2 1 1 0 1 1 1 1 1 2 1 2 0

その他の回答 (1 件)

Voss
Voss 2023 年 10 月 25 日

0 投票

F = [0,1,2];
n_values = numel(F);
n_combos = n_values^n_values;
M = F(1+dec2base(0:n_combos-1,n_values)-'0');
disp(M);
0 0 0 0 0 1 0 0 2 0 1 0 0 1 1 0 1 2 0 2 0 0 2 1 0 2 2 1 0 0 1 0 1 1 0 2 1 1 0 1 1 1 1 1 2 1 2 0 1 2 1 1 2 2 2 0 0 2 0 1 2 0 2 2 1 0 2 1 1 2 1 2 2 2 0 2 2 1 2 2 2

カテゴリ

ヘルプ センター および File ExchangeSimulink Design Optimization についてさらに検索

製品

リリース

R2023b

質問済み:

2023 年 10 月 25 日

編集済み:

2023 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by