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 = [];
0 个评论
采纳的回答
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
2021-12-26
Should the second and thirs ifs be nested under the first one? Probably not. Do this for me.
- Type control-a (to select all the source code text in your editor.
- 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.
更多回答(1 个)
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
Try.
0 个评论
另请参阅
类别
在 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!