0

I have the letters "A", "B", "C", "D", and "E" and I want to generate all possible strings of length 7 with these letters (redundancy allowed). So, I would like to get:

AAAAAAA

AAAAAAB

AAAAAAC

AAAAAAD

AAAAAAE

...

So on and so forth with all possible strings. I know how to do this manually via the following, create:

A = ['A'], B = ['B'], etc...then create embedded for loops to concatenate all elements. However, I would like to just feed in a generic list of ABCDE in a function, and just feed the function an integer to get a variable result. How could I do this?

Alvin Nunez
  • 247
  • 1
  • 10
  • Maybe Luis' answer to [this](http://stackoverflow.com/questions/22329725/all-possible-combinations-of-strings-matlab) question will help. I think. – Benoit_11 Sep 30 '15 at 14:24
  • @Benoit_11 Thank you for your reply. While this Luis' answer and that question are very good references, the implementation does not allow for indexed repeats, such as "bridgebridgebridge". – Alvin Nunez Sep 30 '15 at 14:33
  • Apply [this answer](http://stackoverflow.com/a/21895344/2586922) with input `vectors = repmat({'ABCDE'},1,7);` – Luis Mendo Sep 30 '15 at 14:37

1 Answers1

0

Something like this?

Test = 'ABCDE'; 
A  = cell(7, 1);    %//pre-allocating for speed
[A{:}] = ndgrid(Test);  
y = cellfun(@(Test) {Test(:)} , A);
y = horzcat (y{:}); 

>>  
AAAAAAA
BAAAAAA
CAAAAAA
DAAAAAA

EDIT: ops... didn't see the 7..

GameOfThrows
  • 4,510
  • 2
  • 27
  • 44