make the for loop run faster

조회 수: 1 (최근 30일)
Asliddin Komilov
Asliddin Komilov 2019년 7월 14일
댓글: Asliddin Komilov 2019년 7월 14일
I would appreciate any help to optimize this code since it runs 86400x365x19x37 times:
for ii = 1 : length(time)
if time(ii)<=sunrise
Ta(ii)=Tmin;
elseif time(ii)>=sunrise && time(ii)<=Tmax_time
Ta(ii)=Tmin+(Tmax-Tmin)*sin((time(ii)-sunrise)/(Tmax_time-sunrise)*pi/2);
elseif time(ii)>Tmax_time && time(ii)<=sunset
Ta(ii)=T0+(Tmax-T0)*sin(pi/2+(time(ii)-Tmax_time)*pi/(2*4));
elseif time(ii)>sunset && time(ii)<=Day_end
Ta(ii)=T0+(Tmin-T0)/(Day_end-sunset).^0.75*(time(ii)-sunset).^0.75;
end
end
thanks

채택된 답변

infinity
infinity 2019년 7월 14일
Hello,
How about if you try to use this
time = 1:100;
T0 = 0;
sunrise = 10;
Tmin = 1;
Tmax = 100;
Tmax_time = 50;
sunset = 70;
Day_end = 100;
Ta = (time <= sunrise).* Tmin...
+ (time > sunrise & time <=Tmax_time).*...
(Tmin+(Tmax-Tmin)*sin((time-sunrise)/(Tmax_time-sunrise)*pi/2)) ...
+ (time > Tmax_time & time <=sunset).*...
(T0+(Tmax-T0)*sin(pi/2+(time-Tmax_time)*pi/(2*4)))...
+ (time>sunset & time<=Day_end).*...
(T0+(Tmin-T0)/(Day_end-sunset).^0.75*(time-sunset).^0.75);
You could apply for your data of "time, T0, sunrise, etc.".
  댓글 수: 1
Asliddin Komilov
Asliddin Komilov 2019년 7월 14일
Elapsed time is 2 times shorter.
thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Oceanography and Hydrology에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by