# Find all possible combinations of numbers with a specific array length

2 views (last 30 days)
Sumara on 16 Nov 2023
Moved: Torsten on 16 Nov 2023
I need to write a code that can find all possible combinations of 4 characters with a output length of 12
e.g. all possible combinations of A, B, C, and D with an output length of 12 would look something like
A A A A A A A A A A A A
A A A A A A A A A A A B
A A A A A A A A A A A C
A A A A A A A A A A A D
A A A A A A A A A A B A
etc
Torsten on 16 Nov 2023
Quite many:
4^12
ans = 16777216
William Rose on 16 Nov 2023
@Jillyn Tittle, @Torsten is exactly right. The teacher could impart just as much understanding with this assignment if they specified output length=5, and thus only 1024 strings.

Voss on 16 Nov 2023
One way:
chars = 'ABCD';
n_length = 12;
n_chars = numel(chars);
result = chars(1+dec2base((0:n_chars^n_length-1),n_chars)-'0')
result = 16777216×12 char array

Bruno Luong on 16 Nov 2023
char(dec2base(0:4^12-1,4)+('A'-'0'))

Torsten on 16 Nov 2023
Moved: Torsten on 16 Nov 2023
M = categorical(["A","B","C","D"]);
combinations(M,M,M,M,M,M,M,M,M,M,M,M)
ans = 16777216×12 table
M M_1 M_2 M_3 M_4 M_5 M_6 M_7 M_8 M_9 M_10 M_11 _ ___ ___ ___ ___ ___ ___ ___ ___ ___ ____ ____ A A A A A A A A A A A A A A A A A A A A A A A B A A A A A A A A A A A C A A A A A A A A A A A D A A A A A A A A A A B A A A A A A A A A A A B B A A A A A A A A A A B C A A A A A A A A A A B D A A A A A A A A A A C A A A A A A A A A A A C B A A A A A A A A A A C C A A A A A A A A A A C D A A A A A A A A A A D A A A A A A A A A A A D B A A A A A A A A A A D C A A A A A A A A A A D D

### Categories

Find more on Whos in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!