Skip analysis when you have NaN or 0

조회 수: 7 (최근 30일)
Luccas S.
Luccas S. 2021년 12월 26일
댓글: Luccas S. 2021년 12월 26일
I think I am having problems with my analysis result due to the presence of NaN and 0. How to skip analysis when I have NaN or 0?
p_fix = [];
for n = 4:size(t,1)
X = [Ia(n-1,1) Ia(n-2,1) ; Ia(n-2,1) Ia(n-3,1)];
future = [Ia(n,1) ; Ia(n-1,1)];
C = X\future;
Ia_future(n,1) = C(1,1)*Ia(n,1)+C(2,1)*Ia(n-1,1);
PE(n,1)=Ia(n,1)+Ia_future(n,1);
p(n,1)=(1+0.2)*max(PE(n-1,1));
if isempty(p_fix) && PE(n-1,1)>p(n,1)
p_fix = p(n,1);
if (mod(N,5)==0) && (PE(n-1,1)>p_fix)
fprintf('Accuses IC\n')
else
fprintf('Does not accuse IC\n')
p_fix = [];
end
end
Basically I wanted this part to be ignored when (PE=NaN or 0) and (p = NaN or 0) happened. And perform the PE and p calculations again.
if isempty(p_fix) && PE(n-1,1)>p(n,1)
p_fix = p(n,1);
if (mod(N,5)==0) && (PE(n-1,1)>p_fix)
fprintf('Accuses IC\n')
else
fprintf('Does not accuse IC\n')
p_fix = [];

채택된 답변

Image Analyst
Image Analyst 2021년 12월 26일
Do you want to break out of the loop and go somewhere else,
PE(n,1)=Ia(n,1)+Ia_future(n,1);
p(n,1)=(1+0.2)*max(PE(n-1,1));
if (isnan(PE(n, 1)) || PE(n, 1) == 0) && (isnan(p(n, 1)) || p(n, 1) == 0)
break; % Quit the loop totally
end
or do you want to continue with the next iteration of the loop?
PE(n,1)=Ia(n,1)+Ia_future(n,1);
p(n,1)=(1+0.2)*max(PE(n-1,1));
if (isnan(PE(n, 1)) || PE(n, 1) == 0) && (isnan(p(n, 1)) || p(n, 1) == 0)
continue; % Skip to bottom of loop and continue the loop with the next iteration.
end
  댓글 수: 2
Image Analyst
Image Analyst 2021년 12월 26일
Should the second and thirs ifs be nested under the first one? Probably not. Do this for me.
  1. Type control-a (to select all the source code text in your editor.
  2. Type control-i (to properly indent the if blocks).
Do they all look like they are nested as you want them to be? Probably not.
Luccas S.
Luccas S. 2021년 12월 26일
Oh sorry, I ended up deleting the comment because I had found my mistake. That's right, it was the position of the end.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

William Rose
William Rose 2021년 12월 26일
a=1; %try replacing "1" with "0" or "NaN"
if ~(isnan(a) || a==0)
disp('a is not NaN and is not 0.')
else
disp(a)
end
a is not NaN and is not 0.
Try.

카테고리

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