嵌套用于循环查询

时间:2013-03-19 02:01:56

标签: matlab loops for-loop nested

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

我有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))

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

ConditionACase1Rice ConditionACase1Beans ConditionACase1Carrots ConditionACase1Cereal ConditionACase1Tomato ConditionACase1Cabbage ConditionACase1Sugar ConditionACase2Rice 等等等......

我知道:

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 

但这不起作用......

如果能得到这方面的帮助,我将非常感激。

提前致谢!

3 个答案:

答案 0 :(得分: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之前检查您的代码,因为这是一个非常简单的调试

答案 1 :(得分: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

答案 2 :(得分: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'

如果您需要进一步的帮助,请与我们联系。