필터 지우기
필터 지우기

optimizing for loop that is been called often

조회 수: 1 (최근 30일)
Asliddin Komilov
Asliddin Komilov 2020년 2월 8일
댓글: Asliddin Komilov 2020년 2월 8일
can this loop be optimized? It is called 1m times and is the only thing I couldn't optimize yet.
Oper_year=1:1:Lifetime;
[costM,PVcost,YearProd,Elcost]=deal(zeros(size(Oper_year)));
for j=1:1:length(Oper_year)
if j==1
costM(j)=Mcost*PVprice*PVP;
PVcost(j)=PVprice*PVP+Bcost;
YearProd(j)=sum(PMDb)*PVP;
Elcost(j)=Elprice;
Bcost(j)=Bcost;
else
Bcost(j)=Bcost(1)*(1+bank*Oper_year(j-1));
costM(j)=PVprice*(Mcost*(1+Minf)*(Oper_year(j-1))+Mcost)*PVP;
PVcost(j)=(PVprice*PVP)*(1+bank*Oper_year(j-1))+Bcost(j);
YearProd(j)=sum(PMDb)*PVP*((1-PVdeg)*Oper_year(j-1)+1);
end
end
thanks

채택된 답변

KSSV
KSSV 2020년 2월 8일
Oper_year=1:1:Lifetime;
[costM,PVcost,YearProd,Elcost]=deal(zeros(size(Oper_year)));
costM(1)=Mcost*PVprice*PVP;
PVcost(1)=PVprice*PVP+Bcost;
YearProd(1)=sum(PMDb)*PVP;
Elcost(1)=Elprice;
Bcost(1)=Bcost;
Bcost(2:end)=Bcost(1)*(1+bank*Oper_year(1:end-1));
costM(2:end)=PVprice*(Mcost*(1+Minf)*(Oper_year(1:end-1))+Mcost)*PVP;
PVcost(2:end)=(PVprice*PVP)*(1+bank*Oper_year(1:end-1))+Bcost(2:end);
YearProd(2:end)=sum(PMDb)*PVP*((1-PVdeg)*Oper_year(1:end-1)+1);
  댓글 수: 5
KSSV
KSSV 2020년 2월 8일
The error is becasue the variable Bcost has been treated as scalar as well as vector. Rename the scalar Bcost to some other name. I followed like below:
% some random values for demo
Lifetime = 12 ;
Mcost = rand ;
PVprice = rand ;
PVP = rand ;
BCost = rand ;
PMDb = rand ;
Elprice = rand ;
bank = rand ;
Minf = rand ;
PVdeg = rand ;
Oper_year=1:1:Lifetime;
[costM,PVcost,YearProd,Elcost,Bcost]=deal(zeros(size(Oper_year)));
costM(1)=Mcost*PVprice*PVP;
PVcost(1)=PVprice*PVP+BCost;
YearProd(1)=sum(PMDb)*PVP;
Elcost(1)=Elprice;
Bcost(1)=BCost;
Bcost(2:end)=Bcost(1)*(1+bank*Oper_year(1:end-1));
costM(2:end)=PVprice*(Mcost*(1+Minf)*(Oper_year(1:end-1))+Mcost)*PVP;
PVcost(2:end)=(PVprice*PVP)*(1+bank*Oper_year(1:end-1))+Bcost(2:end);
YearProd(2:end)=sum(PMDb)*PVP*((1-PVdeg)*Oper_year(1:end-1)+1);
Asliddin Komilov
Asliddin Komilov 2020년 2월 8일
I got the point, I will see if it will be faster than the existing code. Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by