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!