所有可能的字符串组合MATLAB

bra*_*nkz 1 string matlab permutation

UPD:我想对StackOverflow社区提出问题表示抱歉,他自己提出问题而没有努力解决问题.从现在开始,只有在我真的遇到严重问题时才会提问

我现在正在开发生成字符串元素的所有可能排列的程序:

我开始时:

A = ['Bridge','No Bridge'];
B = ['Asphalt','Concrete','Combined'];
C = ['Fly Ash',' Sulphur','Nothing'];
D = ['Two lanes','Four lanes with barriers'];
E = ['Paid','Non-paid'];
F = ['Mobile','Non-mobile'];
N = length(A)*length(B)*length(C)*length(D)*length(E)*length(F);
out = zeros(N,6);
Run Code Online (Sandbox Code Playgroud)

但现在我仍然坚持下一步该做什么.所需的输出类似于:

out = 

    'Bridge' 'Asphalt' 'Fly Ash' 'Two lanes' 'Paid' 'Mobile'
    'Bridge' 'Asphalt' 'Fly Ash' 'Two lanes' 'Paid' 'Non-mobile'
    'Bridge' 'Asphalt' 'Fly Ash' 'Two lanes' 'Non-paid' 'Mobile'
    'Bridge' 'Asphalt' 'Fly Ash' 'Two lanes' 'Non-paid' 'Non-mobile' etc
Run Code Online (Sandbox Code Playgroud)

拜托,您能建议最有效的方法吗?

Lui*_*ndo 5

使用ndgrid生成指数的所有组合,然后利用这些索引建立从字符串的结果:

A = {'Bridge','No Bridge'};
B = {'Asphalt','Concrete','Combined'};
C = {'Fly Ash',' Sulphur','Nothing'};
D = {'Two lanes','Four lanes with barriers'};
E = {'Paid','Non-paid'};
F = {'Mobile','Non-mobile'}; %// data. Cell arrays of strings

[a b c d e f] = ndgrid(1:numel(A),1:numel(B),1:numel(C),1:numel(D),1:numel(E),1:numel(F));
out = [A(a(:)).' B(b(:)).' C(c(:)).' D(d(:)).' E(e(:)).' F(f(:)).'];
Run Code Online (Sandbox Code Playgroud)

或者,如果您需要按示例顺序的结果:

[f e d c b a] = ndgrid(1:numel(F),1:numel(E),1:numel(D),1:numel(C),1:numel(B),1:numel(A));
out = [A(a(:)).' B(b(:)).' C(c(:)).' D(d(:)).' E(e(:)).' F(f(:)).'];
Run Code Online (Sandbox Code Playgroud)

这给了

out = 

    'Bridge'       'Asphalt'     'Fly Ash'     'Two lanes'    'Paid'        'Mobile'    
    'Bridge'       'Asphalt'     'Fly Ash'     'Two lanes'    'Paid'        'Non-mobile'
    'Bridge'       'Asphalt'     'Fly Ash'     'Two lanes'    'Non-paid'    'Mobile'    
    'Bridge'       'Asphalt'     'Fly Ash'     'Two lanes'    'Non-paid'    'Non-mobile'
    ...
Run Code Online (Sandbox Code Playgroud)