make the for loop run faster

2 次查看(过去 30 天)
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.".

更多回答(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