speed up for and if loop
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Jan
am 3 Aug. 2022
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
am 3 Aug. 2022
"...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."
Antworten (1)
Jan
am 3 Aug. 2022
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
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!