Is there any alternative to if statements inside for loops?

5 ビュー (過去 30 日間)
Theo
Theo 2012 年 5 月 17 日
Hello, my code has many parts like this :
for i=1:8760
for j=1:Ni
if Ns*Vm(i) < Vi_min
Pin(i,j) = 0;
else
if year==1
r_y=0;
else
r_y=r_pv;
end
eta_m=eta_mppt(Ns*Np*Pm(i));
Pin(i,j) = (1 - As_I(i,j)*SIF/Ns*Np*Lpv1*Lpv2)*(1-n_l_dc*PL_dc)*Ns*Np*eta_m*(1 - r_y*year)*Pm(i);
end
end
end
It takes a long time to execute due to the if statement inside for loops. I would like to ask if there is an alternate implementation to that. Thanks
  1 件のコメント
James Tursa
James Tursa 2012 年 5 月 17 日
Please use formatting for your question so we can read the code.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2012 年 5 月 17 日
1) logical indexing
2) Ns, Vm(i), vi_min are all invariant in your "j" loop, so there is no point doing the "j" loop in that condition. Likewise, eta_m is independent of j.
if Ns*vm(i) < Vi_min
Pin(i,1:ni) = 0;
else
eta_m=eta_mppt(Ns*Np*Pm(i));
for j = 1 : Ni
....
end
end
  1 件のコメント
Jan
Jan 2012 年 5 月 17 日
Walter, you have been faster. +1

サインインしてコメントする。

その他の回答 (3 件)

Dr. Seis
Dr. Seis 2012 年 5 月 17 日
Do you pre-allocated "Pin", if not... then before the for-loops put:
Pin = zeros(8760,Ni);
The inside of your for-loops will become:
if Ns*Vm(i) >= Vi_min
if year==1
r_y=0;
else
r_y=r_pv;
end
eta_m=eta_mppt(Ns*Np*Pm(i));
Pin(i,j) = (1 - As_I(i,j)*SIF/Ns*Np*Lpv1*Lpv2)*...
(1-n_l_dc*PL_dc)*Ns*Np*eta_m*(1 - r_y*year)*Pm(i);
end
That should help run-time a bit.

Jan
Jan 2012 年 5 月 17 日
The if -condition does not depend on the counter of the inner loop:
for j=1:Ni
if Ns*Vm(i) < Vi_min
Then you can move it outside:
if Ns*Vm(i) >= Vi_min
for j=1:Ni
The pre-allocation mentioned by Elige will be more important.

Theo
Theo 2012 年 5 月 17 日
Thanks a lot!! , there has been a great improvement. Last question , is there anything else that can be done about those 4 for loops? I appreciate your help.
for i=1:8760
for j=1:Ni
if Pin(i,j)>Pi_sc
if Pin(i,j)<=Pi_na(i)
eta=eta_inv(Pin(i,j));
Po(i,j) = eta*Pin(i,j);
else
eta=eta_inv(Pi_na(i));
Po(i,j) = eta*Pi_na(i);
end
else Po(i,j) = 0;
end
end
end
for i=1:8760
if P_plant(i)>P_plant_max
P_plant(i) = P_plant_max;
end
end
for i=1:8760
if P_plant(i) > P_grid_max(i)*(1 + (year-1)*Sg)
P_plant(i) = P_grid_max(i)*(1 + (year-1)*Sg);
end
end
for i=1:8760
for j=1:Ni
if Pin(i,j) < 0
Pin(i,j) = 0;
end
end
end
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 5 月 17 日
Well, for example, you can replace
for i=1:8760
if P_plant(i)>P_plant_max
P_plant(i) = P_plant_max;
end
end
with
P_plant = min(P_plant, P_plant_max);
with no loop.
Likewise, consider
Pin = max(Pin,0);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by