矢量化For循环Matlab代码中的错误

时间:2015-05-10 04:41:44

标签: matlab if-statement for-loop parfor

我有一个我想要矢量化的for循环代码。下面是循环代码的初始化,以及代码的矢量化版本。矢量化代码没有给出与parfor循环相同的结果,因此我知道代码有问题。如果论坛的任何成员可以帮助我查看矢量化代码并查看他们是否可以向我指出我的错误,我将不胜感激。提前谢谢。

%初始化和预计算 %w是n×1载体 %beta:任何大于0的数字。通常设置为1。 这是我需要矢量化的for循环代码:

f = zeros(n,1);
x = w;
y = w;
rho = 1;
v = f – (rho*y);
rhow = rho*w;
n = length(w);

parfor i = 1 : n

if w(i) >= 0
    if v(i) < -rhow(i) – beta – 1
        x(i) = (-beta -1 -v(i))/rho;

    elseif (-rhow(i) – beta – 1 <= v(i)) && (v(i) <= -rhow(i) + beta – 1)
        x(i) = w(i);

    elseif (-rhow(i) + beta – 1 < v(i)) && (v(i) < beta – 1)
        x(i) = (beta – 1 -v(i))/rho;

    elseif (beta – 1 <= v(i)) && (v(i) <= beta + 1)
        x(i) = 0;

    else
        x(i) = (beta + 1 – v(i))/rho;
    end

else

    if v(i) < -beta -1
        x(i) = (-beta -1 – v(i))/rho;

    elseif (-beta – 1 <= v(i) )&& (v(i) <= -beta + 1)
        x(i) = 0;

    elseif (-beta + 1 < v(i)) && (v(i) < -rhow(i) – beta + 1)
        x(i) = (-beta + 1 – v(i))/rho;

    elseif (-rhow(i) – beta + 1 <= v(i)) && (v(i) <= -rhow(i) + beta + 1)
        x(i) = w(i);

    else
        x(i) = (beta + 1 – v(i))/rho;
    end

end

=============================================== =======================

这是我上面代码的矢量化版本:

cond1 = (w >= 0);
cond2 = (w >= 0) & (v < -rhow-beta-1);       
x(cond2) = (-beta-1-v(cond2))/rho; 

cond3 = (w>=0)&(-rhow - beta -1 <= v) & (v <= -rhow + beta - 1);
x(cond3) =  w(cond3);

cond4 = (w>=0) & (-rhow +beta - 1 < v) & (v < beta - 1);
x(cond4) = (beta - 1 - v(cond4))/rho;

cond5 = (w>=0) & (beta - 1 <= v) & (v <= beta + 1);
x(cond5) = 0;

cond6 = (~cond2);
x(cond6) = (beta + 1 - v(cond6))/rho;

cond7 = ((~cond1) & v < -beta -1);
x(cond7) = (-beta -1 - v(cond7))/rho;

cond8 = ((~cond1) & (-beta - 1 <= v) & (v <= -beta + 1));
x(cond8) = 0;

cond9 = ((~cond1) & (-beta + 1 < v) & (v < -rhow - beta + 1));
x(cond9) = (-beta + 1 - v(cond9))/rho;

cond10 = ((~cond1) & (-rhow - beta + 1 <= v) & (v <= -rhow + beta + 1));
x(cond10) = w(cond10);

cond11 = (~cond1);
x(cond11) = (beta + 1 - v(cond11))/rho;

2 个答案:

答案 0 :(得分:0)

这是一个错误,cond6不等同于原来的第一个The content ('23', 'UI-iAs-23913618634441256\0\0\0\0\0\0')

else
原来这是:

cond2 = (w >= 0) & (v < -rhow-beta-1);
cond6 = (~cond2);
x(cond6) = (beta + 1 - v(cond6))/rho;

其他应该像这样评估(如果我没有记错的话)

if w(i) >= 0
   if v(i) < -rhow(i) – beta – 1
      ...
   else
      x(i) = (beta + 1 – v(i))/rho;  %this should be cond6
   end
end

并且在所有其他人之前达到cond5。

我没有检查所有代码,所以如果这不能解决您的问题,请告诉我。

答案 1 :(得分:0)

我在检查所有条件时添加了另一个答案:

cond1 = (w >= 0);
cond2 = cond1 & (v < -rhow – beta – 1);
cond3 = cond1 & ((-rhow – beta – 1 <= v) && (v <= -rhow + beta – 1));
cond4 = cond1 & ((-rhow + beta – 1 < v) && (v < beta – 1));
cond5 = cond1 & ((beta – 1 <= v) && (v <= beta + 1));
cond6 = cond1 & (v > beta + 1)

cond7 = ~cond1 & (v < -beta -1);
cond8 = ~cond1 & ((-beta – 1 <= v ) && (v <= -beta + 1));
cond9 = ~cond1 & ((-beta + 1 < v) && (v < -rhow – beta + 1));
cond10 = ~cond1 & ((-rhow – beta + 1 <= v) && (v <= -rhow + beta + 1));
cond11 = ~cond1 & (v > -rhow + beta + 1);

x(cond2)= ...到x(cond11)= ...保持不变。 希望这有效。