speed up for and if loop
2 次查看(过去 30 天)
显示 更早的评论
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
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
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
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!