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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by