speed up for and if loop

1 次查看(过去 30 天)
Frederik Reese
Frederik Reese 2022-8-3
Hi,
I want to get better in programming.
my Code works well but how can I speed up the calculation?
Thanks in andvance
for y=1:size(Wasserhoehe_HQ10000,1)%4585
for i= 1:10000% 2: size(FRI_10000_EventPhase,2);% 155
if isnan(Zeit_Flutende_10000_BA(y,:))==1
FRI_RecoveryPhase_10000(y,:)=nan;
else
if i == 1 % für ersten FRI Recovery letzten FRI Event als start
FRI_RecoveryPhase_10000(y,1)=FRI_10000_EventPhase(y,Zeit_Flutende_10000_BA(y,:)-1 ).*RF_10000(y,:);
else
FRI_RecoveryPhase_10000(y,[i])=FRI_RecoveryPhase_10000(y,i-1).*RF_10000(y,:); %4585X155
end
end
end
end
  4 个评论
Jan
Jan 2022-8-3
There are no "if loop" in any programming language I know.
Do not store data in the name of variables. "Wasserhoehe_HQ10000" is too long to be readable. All variables contain a "10000", so this is redundant and should be omitted.
"rows which are nan" - this is still not clear: do you mean, that all elements of this row are NaN's or if any element is NaN?
Bruno Luong
Bruno Luong 2022-8-3
"...that all elements of this row are NaN's or if any element is NaN?"
it's the same since
"Zeit_Flutende_10000_BA has only one column."

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2022-8-3
isnan replies trtue or false. There is no need to compare it with ==1 afterwards.
nWasserhoehe = size(Wasserhoehe_HQ, 1);
FRI_RecoveryPhase = zeros(nWasserhoehe, 10000); % Pre-allocation!!!
for y = 1:nWasserhoehe % 4585
RFy = RF(y, :); % Temporary variable
% für ersten FRI Recovery letzten FRI Event als start
FRI_RecoveryPhase(y, 1) = FRI_EventPhase(y, Zeit_Flutende_BA(y, :) - 1) .* RFy;
for i = 2:10000 % 2: size(FRI_EventPhase,2);% 155
if isnan(Zeit_Flutende_BA(y, :)) % any() or all() ?!?
FRI_RecoveryPhase(y, :) = nan;
else
FRI_RecoveryPhase(y, i) = FRI_RecoveryPhase(y, i - 1) .* RFy; %4585X155
end
end
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by