0

我正在尝试做一些相当简单的事情,但似乎无法得到它......

我有 3 个带字符串的单元格数组,

A = {'ConditionA'; 'ConditionB'; 'ConditionC'; 'ConditionD'};

B = {'Case1'; 'Case2'; 'Case3'; 'Case4'};

C = {'Rice'; 'Beans'; 'Carrots'; 'Cereal';'Tomato'; 'Cabbage';...
    'Sugar'}

我想用连接的(strcat?)组合生成一个向量,因为它是一个“树形图”,例如:

strcat(A(1),B(1),C(1))
strcat(A(1),B(1),C(2))
strcat(A(1),B(1),C(3))
strcat(A(1),B(1),C(4))
strcat(A(1),B(1),C(5))
strcat(A(1),B(1),C(6))
strcat(A(1),B(1),C(7))

strcat(A(1),B(2),C(1))

那么我想要获得的第一个元素是(理想情况下在列中):

条件ACase1大米条件ACase1豆子条件ACase1胡萝卜条件ACase1谷物条件ACase1番茄条件ACase1卷心菜条件ACase1糖条件ACase2大米等等等...

我知道:

for i=1:length(A)
    E(i) = strcat(A(i),B(1),C(1))
end

适用于一个“级别”。我努力了:

for i=1:length(A)
    for j=1:length(B)
        for k=1:length(C)
            P(i) = strcat(A(i),B(j),C(k));
        end
    end
end 

但这不起作用...

如果我能在这方面得到帮助,我将不胜感激。

提前致谢!

4

3 回答 3

0

{

x = 1;
    for i=1:length(A)
        for j=1:length(B)
            for k=1:length(C)
                P(x) = strcat(A(i),B(j),C(k));
                x = x + 1;
            end
        end
    end

}

请基本上在发布到 PO 之前检查您的代码,因为这是一个非常简单的调试

于 2013-03-19T02:12:28.493 回答
0

我对matlab不是很熟悉..但也许可以试试这样的东西?

for A = {'ConditionA'; 'ConditionB'; 'ConditionC'; 'ConditionD'};
   for B = {'Case1'; 'Case2'; 'Case3'; 'Case4'};
     for  C = {'Rice'; 'Beans'; 'Carrots'; 'Cereal';'Tomato'; 'Cabbage'; 'Sugar'}
       P(i) = strcat(A(i),B(j),C(k));
    end
   end
  end
于 2013-03-19T02:15:34.107 回答
0

据我了解,您需要指定的输入数组字符串的所有可能组合。如果是这样,只需将嵌套循环替换为以下内容:

P = cell(length(A)*length(B)*length(C),1);
t=1;
for i=1:length(A)
    for j=1:length(B)
        for k=1:length(C)
            P(t) = strcat(A(i),B(j),C(k));
            t = t+1;
        end
    end
end

对于输入数组,

>> A = {'ConditionA'; 'ConditionB'; 'ConditionC'; 'ConditionD'};
>> B = {'Case1'; 'Case2'; 'Case3'; 'Case4'};
>> C = {'Rice'; 'Beans'; 'Carrots'; 'Cereal';'Tomato'; 'Cabbage';'Sugar'};

P 的值是:

>> P

P = 

    'ConditionACase1Rice'
    'ConditionACase1Beans'
    'ConditionACase1Carrots'
    'ConditionACase1Cereal'
    'ConditionACase1Tomato'
    'ConditionACase1Cabbage'
    'ConditionACase1Sugar'
    'ConditionACase2Rice'
    'ConditionACase2Beans'
    'ConditionACase2Carrots'
    'ConditionACase2Cereal'
    'ConditionACase2Tomato'
    'ConditionACase2Cabbage'
    'ConditionACase2Sugar'
    'ConditionACase3Rice'
    'ConditionACase3Beans'
    'ConditionACase3Carrots'
    'ConditionACase3Cereal'
    'ConditionACase3Tomato'
    'ConditionACase3Cabbage'
    'ConditionACase3Sugar'
    'ConditionACase4Rice'
    'ConditionACase4Beans'
    'ConditionACase4Carrots'
    'ConditionACase4Cereal'
    'ConditionACase4Tomato'
    'ConditionACase4Cabbage'
    'ConditionACase4Sugar'
    'ConditionBCase1Rice'
    'ConditionBCase1Beans'
    'ConditionBCase1Carrots'
    'ConditionBCase1Cereal'
    'ConditionBCase1Tomato'
    'ConditionBCase1Cabbage'
    'ConditionBCase1Sugar'
    'ConditionBCase2Rice'
    'ConditionBCase2Beans'
    'ConditionBCase2Carrots'
    'ConditionBCase2Cereal'
    'ConditionBCase2Tomato'
    'ConditionBCase2Cabbage'
    'ConditionBCase2Sugar'
    'ConditionBCase3Rice'
    'ConditionBCase3Beans'
    'ConditionBCase3Carrots'
    'ConditionBCase3Cereal'
    'ConditionBCase3Tomato'
    'ConditionBCase3Cabbage'
    'ConditionBCase3Sugar'
    'ConditionBCase4Rice'
    'ConditionBCase4Beans'
    'ConditionBCase4Carrots'
    'ConditionBCase4Cereal'
    'ConditionBCase4Tomato'
    'ConditionBCase4Cabbage'
    'ConditionBCase4Sugar'
    'ConditionCCase1Rice'
    'ConditionCCase1Beans'
    'ConditionCCase1Carrots'
    'ConditionCCase1Cereal'
    'ConditionCCase1Tomato'
    'ConditionCCase1Cabbage'
    'ConditionCCase1Sugar'
    'ConditionCCase2Rice'
    'ConditionCCase2Beans'
    'ConditionCCase2Carrots'
    'ConditionCCase2Cereal'
    'ConditionCCase2Tomato'
    'ConditionCCase2Cabbage'
    'ConditionCCase2Sugar'
    'ConditionCCase3Rice'
    'ConditionCCase3Beans'
    'ConditionCCase3Carrots'
    'ConditionCCase3Cereal'
    'ConditionCCase3Tomato'
    'ConditionCCase3Cabbage'
    'ConditionCCase3Sugar'
    'ConditionCCase4Rice'
    'ConditionCCase4Beans'
    'ConditionCCase4Carrots'
    'ConditionCCase4Cereal'
    'ConditionCCase4Tomato'
    'ConditionCCase4Cabbage'
    'ConditionCCase4Sugar'
    'ConditionDCase1Rice'
    'ConditionDCase1Beans'
    'ConditionDCase1Carrots'
    'ConditionDCase1Cereal'
    'ConditionDCase1Tomato'
    'ConditionDCase1Cabbage'
    'ConditionDCase1Sugar'
    'ConditionDCase2Rice'
    'ConditionDCase2Beans'
    'ConditionDCase2Carrots'
    'ConditionDCase2Cereal'
    'ConditionDCase2Tomato'
    'ConditionDCase2Cabbage'
    'ConditionDCase2Sugar'
    'ConditionDCase3Rice'
    'ConditionDCase3Beans'
    'ConditionDCase3Carrots'
    'ConditionDCase3Cereal'
    'ConditionDCase3Tomato'
    'ConditionDCase3Cabbage'
    'ConditionDCase3Sugar'
    'ConditionDCase4Rice'
    'ConditionDCase4Beans'
    'ConditionDCase4Carrots'
    'ConditionDCase4Cereal'
    'ConditionDCase4Tomato'
    'ConditionDCase4Cabbage'
    'ConditionDCase4Sugar'

如果您需要进一步的帮助,请告诉我。

于 2013-03-19T04:13:57.857 回答