Find all possible combinations of string array

35 views (last 30 days)
Hi,
i have a struct that contains cells of different dimensions. I need a way to find all possible combinations of all cells.
The cells containing string variable e.g: matrix(1).elements = {'500A', '600B', '700C',''}, matrix(2).elements = {'100AC', '300D', '200CA','60G'}, matrix(3).elements = {'100BC', '300DF', '200AA','60GR'}...(some cells have less variables and for this reason i have " at the last element of matrix(1))
The total number of combinations in the example above is: 4*4*4=64
The results that i would like is a mat like: 1) 500A,100AC, 100BC,
2) 500A,100AC, 300DF
3) 500A,100AC, 200AA...
I tried allcomb function but it doesn't work
Any idea on that?
Thanks in advance

Accepted Answer

Adam Danz
Adam Danz on 7 Dec 2021
Edited: Adam Danz on 9 Dec 2021
Here are two options.
Use combvec() from the Deep Learning Toolbox
a = {'a','b','c','d'};
b = {'qq','rrr','ss'};
c = {'tuv','xyz'};
m = combvec(1:numel(a), 1:numel(b), 1:numel(c))
m = 3×24
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2
combStr = [a(m(1,:)); b(m(2,:)); c(m(3,:))]'
combStr = 24×3 cell array
{'a'} {'qq' } {'tuv'} {'b'} {'qq' } {'tuv'} {'c'} {'qq' } {'tuv'} {'d'} {'qq' } {'tuv'} {'a'} {'rrr'} {'tuv'} {'b'} {'rrr'} {'tuv'} {'c'} {'rrr'} {'tuv'} {'d'} {'rrr'} {'tuv'} {'a'} {'ss' } {'tuv'} {'b'} {'ss' } {'tuv'} {'c'} {'ss' } {'tuv'} {'d'} {'ss' } {'tuv'} {'a'} {'qq' } {'xyz'} {'b'} {'qq' } {'xyz'} {'c'} {'qq' } {'xyz'} {'d'} {'qq' } {'xyz'} {'a'} {'rrr'} {'xyz'} {'b'} {'rrr'} {'xyz'} {'c'} {'rrr'} {'xyz'} {'d'} {'rrr'} {'xyz'} {'a'} {'ss' } {'xyz'} {'b'} {'ss' } {'xyz'} {'c'} {'ss' } {'xyz'} {'d'} {'ss' } {'xyz'}
Use ndgrid, no toolboxes needed
[ai,bi,ci] = ndgrid(1:numel(a), 1:numel(b), 1:numel(c));
m = [ai(:),bi(:),ci(:)]'
m = 3×24
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2
combStr2 = [a(m(1,:)); b(m(2,:)); c(m(3,:))]'
combStr2 = 24×3 cell array
{'a'} {'qq' } {'tuv'} {'b'} {'qq' } {'tuv'} {'c'} {'qq' } {'tuv'} {'d'} {'qq' } {'tuv'} {'a'} {'rrr'} {'tuv'} {'b'} {'rrr'} {'tuv'} {'c'} {'rrr'} {'tuv'} {'d'} {'rrr'} {'tuv'} {'a'} {'ss' } {'tuv'} {'b'} {'ss' } {'tuv'} {'c'} {'ss' } {'tuv'} {'d'} {'ss' } {'tuv'} {'a'} {'qq' } {'xyz'} {'b'} {'qq' } {'xyz'} {'c'} {'qq' } {'xyz'} {'d'} {'qq' } {'xyz'} {'a'} {'rrr'} {'xyz'} {'b'} {'rrr'} {'xyz'} {'c'} {'rrr'} {'xyz'} {'d'} {'rrr'} {'xyz'} {'a'} {'ss' } {'xyz'} {'b'} {'ss' } {'xyz'} {'c'} {'ss' } {'xyz'} {'d'} {'ss' } {'xyz'}
Compare results
isequal(combStr, combStr2)
ans = logical
1
Use ndgrid with a variable number of vectors / cell arrays
% Concatenate all cell arrays into a since
% cell array of cells (data in this example).
a = {'a','b','c','d'};
b = {'qq','rrr','ss'};
c = {'tuv','xyz'};
d = {'1','2','3','4','5'};
data = {a,b,c,d}
data = 1×4 cell array
{1×4 cell} {1×3 cell} {1×2 cell} {1×5 cell}
% Combine all nested cell arrays in 'data'.
indices = cellfun(@(c){1:numel(c)},data);
ndg = cell(size(data));
[ndg{:}] = ndgrid(indices{:});
combStrCellArray = cellfun(@(str,ind){str(ind)},data,ndg);
combStrCell = cellfun(@(c){c(:)}, combStrCellArray);
combStr = horzcat(combStrCell{:})'
combStr = 4×120 cell array
{'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'a' } {'b' } {'c' } {'d' } {'qq' } {'qq' } {'qq' } {'qq' } {'rrr'} {'rrr'} {'rrr'} {'rrr'} {'ss' } {'ss' } {'ss' } {'ss' } {'qq' } {'qq' } {'qq' } {'qq' } {'rrr'} {'rrr'} {'rrr'} {'rrr'} {'ss' } {'ss' } {'ss' } {'ss' } {'qq' } {'qq' } {'qq' } {'qq' } {'rrr'} {'rrr'} {'rrr'} {'rrr'} {'ss' } {'ss' } {'ss' } {'ss' } {'qq' } {'qq' } {'qq' } {'qq' } {'rrr'} {'rrr'} {'rrr'} {'rrr'} {'ss' } {'ss' } {'ss' } {'ss' } {'qq' } {'qq' } {'qq' } {'qq' } {'rrr'} {'rrr'} {'rrr'} {'rrr'} {'ss' } {'ss' } {'ss' } {'ss' } {'qq' } {'qq' } {'qq' } {'qq' } {'rrr'} {'rrr'} {'rrr'} {'rrr'} {'ss' } {'ss' } {'ss' } {'ss' } {'qq' } {'qq' } {'qq' } {'qq' } {'rrr'} {'rrr'} {'rrr'} {'rrr'} {'ss' } {'ss' } {'ss' } {'ss' } {'qq' } {'qq' } {'qq' } {'qq' } {'rrr'} {'rrr'} {'rrr'} {'rrr'} {'ss' } {'ss' } {'ss' } {'ss' } {'qq' } {'qq' } {'qq' } {'qq' } {'rrr'} {'rrr'} {'rrr'} {'rrr'} {'ss' } {'ss' } {'ss' } {'ss' } {'qq' } {'qq' } {'qq' } {'qq' } {'rrr'} {'rrr'} {'rrr'} {'rrr'} {'ss' } {'ss' } {'ss' } {'ss' } {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'tuv'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'xyz'} {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'1' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'2' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'3' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'4' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' } {'5' }
  4 Comments
Adam Danz
Adam Danz on 9 Dec 2021
I've updated my answer. See the addendum at the bottom.

Sign in to comment.

More Answers (1)

David Hill
David Hill on 7 Dec 2021
Edited: David Hill on 7 Dec 2021
You could get all possible combinations and then index into your struct.
[idxa,idxb,idxc]=ndgrid(1:5,1:3,1:7);
  1 Comment
Christos Traianos
Christos Traianos on 9 Dec 2021
Thanks David,
How can the ndgrid function be used for multiple inputs vector and consequently multiple outputs?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!