Skip analysis when you have NaN or 0

7 次查看(过去 30 天)
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