speed up for and if loop

2 ビュー (過去 30 日間)
Frederik Reese
Frederik Reese 2022 年 8 月 3 日
コメント済み: 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
  1 件のコメント
Frederik Reese
Frederik Reese 2022 年 8 月 3 日
Thanks

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

カテゴリ

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