matlab - Vectorize For-If-Elseif Loop -


i struggling vectorizing parfor loop. want remove parfor loop code taking long time execute when n large. please see code pasted below. appreciate tips/advice/help person in forum can give me on this. many in advance.

% initialization , precomputations % w n x 1 vector % beta: number larger 0. set 1.  f = zeros(n,1); x = w; y = w; rho = 1; v = f – (rho*y); rhow = rho*w; n = length(w);  parfor = 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 hbderts answer, helped me much. came with. i'm still getting wrong because when slot in values variables, don't desired result have parfor loop. can me @ , let me know got wrong? many 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; 

you can use logical vector index matrix, described in matlab help pages. let's make simple example:

a = [1 2 3 4]; ind = logical([0 1 0 1]); b = a(ind)  b =    2    4 

you can use system model different cases , drop loop. first case, be

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

let's @ term x((w>=0)&(v<-rhow-beta-1)) in detail:

  • w>=0 creates logical vector containing 1 (true) if corresponding entry in w >=0 , 0 (false) otherwise.
  • v<-rhow-beta-1 creates logical vector, containing true or false.
  • the & between these terms logic and. have vector containing true elements wich meet both conditions, , false otherwise.
  • with x(...), elements x, meet both of conditions above.

now have elements want set in first step. have create values set them to. part (-beta-1-v(...))/rho same before. v(...) ... same conditions before, take relevant v's, calculation them , save them in correct position of x.

we can repeat procedure if-then-else clauses have. second one, be

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

and on...


Comments