0

从下面Matlab中的“for循环”中,我想提取150个“规则”矩阵(每个矩阵的维度为1200 * 5),而“数据”的维度为1200 * 5,“Var1C”为150 * 5,“Var2C”为150 *5 尺寸。谢谢。

for i = 1:150,
    for j=1:5,
        for i1=1:1200,
            if Var1C(i,j)==1 & Data(i1,j)<Var2C(i,j) | Var1C(i,j)==2 & Data(i1,j)>=Var2C(i,j)
               Rules = 0;
            else
               Rules = 1;
            end
        end
    end
end
4

2 回答 2

3

好吧,您只需拥有一个 150 x 1200 x 5 矩阵:

Rules = zeros(150,1200,5); % pre-allocate matrix
for i = 1:150,
    for j=1:5,
        for i1=1:1200,
            if Var1C(i,j)==1 & Data(i1,j)<Var2C(i,j) | Var1C(i,j)==2 & Data(i1,j)>=Var2C(i,j)
               Rules(i,i1,j) = 0;
            else
               Rules(i,i1,j) = 1;
            end
        end
    end
end
于 2013-07-17T08:30:20.657 回答
0

为什么不使用这个可怕的嵌套循环进行矢量化?

Rules = bsxfun( @and, permute( Var1C, [1 3 2] ) == 1,...
                      bsxfun( @lt, permute( Data, [3 1 2 ] ), permute( Var2C, [1 3 2] ) ) ) | ...
        bsxfun( @and, permute( Var1C, [1 3 2] ) == 2,...
                      bsxfun( @ge, permute( Data, [3 1 2 ] ), permute( Var2C, [1 3 2] ) ) ) ;
于 2013-07-17T09:18:16.993 回答