Skip analysis when you have NaN or 0

7 vues (au cours des 30 derniers jours)
Luccas S.
Luccas S. le 26 Déc 2021
Commenté : Luccas S. le 26 Déc 2021
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 = [];

Réponse acceptée

Image Analyst
Image Analyst le 26 Déc 2021
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 commentaires
Image Analyst
Image Analyst le 26 Déc 2021
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. le 26 Déc 2021
Oh sorry, I ended up deleting the comment because I had found my mistake. That's right, it was the position of the end.

Connectez-vous pour commenter.

Plus de réponses (1)

William Rose
William Rose le 26 Déc 2021
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.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by