How can I write this code faster in MATLAB? Maybe using vectorizations. Basically I do not know how to vectorize with if. In the following, if an element of the array u is 0, then the while loop happens with an update. If the element is non zero then benefit(c,t,w) = B(c,tprime,w)/u(c,tprime,w); and i(c,t,w) = tprime ; happens.
county_size=3 ; time_size = 3; scenario_size = 3; for c=1:county_size for w=1:scenario_size for t=tt:time_size l = u(c,t,w) ; tprime = t ; while (l==0) if tprime > time_size-1 tprime = time_size + 1 ; B(c,time_size + 1,w) = 0 ; u(c,time_size + 1,w) = 1 ; break end l = u(c,tprime+1,w) ; tprime = tprime + 1 ; end benefit(c,t,w) = B(c,tprime,w)/u(c,tprime,w) ; i(c,t,w) = tprime ; end end end
ttand other required variables