I have a for loop code which I want to vectorize. Below is the initial for loop code, and the vectorized version of the code. The vectorized code isn't giving the same result as that of the parfor loop, hence I know something is wrong with the code. I would appreciate it if any member of the forum can help me review the vectorized code and see if they can point out my errors to me. Thank you in advance.
我有一个for循环代码,我想要矢量化。下面是循环代码的初始化,以及代码的矢量化版本。矢量化代码没有给出与parfor循环相同的结果,因此我知道代码有问题。如果论坛的任何成员可以帮助我查看矢量化代码并查看他们是否可以向我指出我的错误,我将不胜感激。先感谢您。
% Initialization and precomputations % w is an n x 1 vector % beta: any number larger than 0. Usually set to 1. Here is the for-loop code I need to vectorize:
%初始化和预计算%w是n x 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
end
======================================================================
And here is my vectorized version of the code above:
这是我上面代码的矢量化版本:
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 个解决方案
#1
I'm adding another answer with all the conditionals checked:
我在检查所有条件时添加了另一个答案:
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)=... to x(cond11)=... remain the same. Hope this works.
x(cond2)= ...到x(cond11)= ...保持不变。希望这有效。
#2
Here is one mistake, cond6 is not equivalent to the original first else
这是一个错误,cond6不等于原来的第一个
cond2 = (w >= 0) & (v < -rhow-beta-1);
cond6 = (~cond2);
x(cond6) = (beta + 1 - v(cond6))/rho;
in the original this is:
在原来这是:
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
The else should be evaluated like this (If I'm not mistaken)
其他应该像这样评估(如果我没有记错的话)
x(cond1) = (beta + 1 - v(cond6))/rho;
and before all the others up to cond5.
并且在所有其他人之前达到cond5。
I did not check all the code, so if this doesn't solve your problem let me know.
我没有检查所有代码,所以如果这不能解决你的问题,请告诉我。
#1
I'm adding another answer with all the conditionals checked:
我在检查所有条件时添加了另一个答案:
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)=... to x(cond11)=... remain the same. Hope this works.
x(cond2)= ...到x(cond11)= ...保持不变。希望这有效。
#2
Here is one mistake, cond6 is not equivalent to the original first else
这是一个错误,cond6不等于原来的第一个
cond2 = (w >= 0) & (v < -rhow-beta-1);
cond6 = (~cond2);
x(cond6) = (beta + 1 - v(cond6))/rho;
in the original this is:
在原来这是:
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
The else should be evaluated like this (If I'm not mistaken)
其他应该像这样评估(如果我没有记错的话)
x(cond1) = (beta + 1 - v(cond6))/rho;
and before all the others up to cond5.
并且在所有其他人之前达到cond5。
I did not check all the code, so if this doesn't solve your problem let me know.
我没有检查所有代码,所以如果这不能解决你的问题,请告诉我。