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

 채택된 답변

Adam Danz
Adam Danz 2021년 12월 7일
편집: Adam Danz 2021년 12월 9일
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

Hi Adam,
thanks for the answer! it works perfect for 3 cell arrays as above. Although i would like to apply the above script for n cells (not only a,b,c). Could you help me on that?
Example: I would like the following script to be dynamic for n inputs
m = mtcombvec(1:opt.sections(1).N,1:opt.sections(2).N,1:opt.sections(3).N,1:opt.sections(4).N,1:opt.sections(5).N,1:opt.sections(6).N,1:opt.sections(7).N,1:opt.sections(8).N,1:opt.sections(9).N,1:opt.sections(10).N);
combStr = [opt.sections(1).pass_comb(m(1,:)); opt.sections(2).pass_comb(m(2,:)); opt.sections(3).pass_comb(m(3,:)); opt.sections(4).pass_comb(m(4,:)); opt.sections(5).pass_comb(m(5,:)); opt.sections(6).pass_comb(m(6,:)); opt.sections(7).pass_comb(m(7,:)); opt.sections(8).pass_comb(m(8,:)); opt.sections(9).pass_comb(m(9,:)); opt.sections(10).pass_comb(m(10,:))]';
Hi Adam,
How to transform the following script for a,b,c...n inputs and ai,bi,ci....ni outputs?
[ai,bi,ci...ni] = ndgrid(1:numel(a), 1:numel(b), 1:numel(c)... 1:numel(n));
m = [ai(:),bi(:),ci(:)....ni(:)]'
I've updated my answer. See the addendum at the bottom.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

David Hill
David Hill 2021년 12월 7일
편집: David Hill 2021년 12월 7일

0 개 추천

You could get all possible combinations and then index into your struct.
[idxa,idxb,idxc]=ndgrid(1:5,1:3,1:7);

댓글 수: 1

Thanks David,
How can the ndgrid function be used for multiple inputs vector and consequently multiple outputs?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2021년 12월 7일

댓글:

2021년 12월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by