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. The vectorization I have done so far is also posted below. I will appreciate if any person in this forum can help me look at the vectorized codes and let me know where I got it wrong. 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
===================================================================================
cond1 = (w >= 0); cond2 = (cond1) & (v < -rhow-beta-1); x(cond2) = (-beta-1-v(cond2))/rho; cond3 = (cond1)&(-rhow - beta -1 <= v) & (v <= -rhow + beta - 1); x(cond3) = w(cond3); cond4 = (cond1) & (-rhow +beta - 1 < v) & (v < beta - 1); x(cond4) = (beta - 1 - v(cond4))/rho; cond5 = (cond1) & (beta - 1 <= v) & (v <= beta + 1); x(cond5) = 0; cond6 = (cond1) & (~(v < -rhow -beta - 1)); 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) & (~(v < -beta - 1))); x(cond11) = (beta + 1 - v(cond11))/rho;
Best Answer