I am struggling with vectorizing this parfor loop. I want to completely remove the parfor loop from the code as it is taking a long time to execute when n is large. Please see the code pasted below. I will appreciate any tips/advice/help any person in this forum can give me on this. Many thanks in advance.
% Initialization and precomputations % w is an n x 1 vector % beta: any number larger than 0. Usually set to 1. 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 UPDATE: Thank you very much Hbderts for your answer, it helped me very much. This is what I finally came up with. I'm still getting something wrong because when I slot in values for the variables, I don't get the desired result as I have with the parfor loop. Can you help me look at it and let me know where I got it wrong? Many thanks in advance.
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;