필터 지우기
필터 지우기

Why break loop is not working?

조회 수: 4 (최근 30일)
Athira T Das
Athira T Das 2022년 7월 22일
댓글: Walter Roberson 2022년 7월 22일
The value of e at certain points is infinity. But the break loop is not working.
clc; clear all; close all;
M=3
M = 3
for m1=0:M
m1;
for s1=0:1/2:m1/2
s1;
for s2=0:1/2:(M-m1)/2
s2;
if (m1-(2*s2)) < 0
break
end
for j1=0:m1-2*s1
j1;
if gamma(1+m1-(2*s2-j1))== nan;
break
end
if gamma(1+m1-(2*s2-j1))== Inf;
break
end
if gamma(1+m1-(2*s2-j1)) == 0
break
end
for j2=0:(M-m1-(2*s2))
j2;
q=factorial(m1-(2.*s2));
w=factorial(j1);
e=gamma(1+m1-(2.*s2)-j1)
end
end
end
end
end
e = 1
e = 1
e = 1
e = 1
e = 1
e = 1
e = 1
e = 1
e = 1
e = 1
e = 1
e = 1
e = Inf
e = Inf
e = 1
e = 1
e = 1
e = 1
e = 1
e = 2
e = 2
e = 1
e = 1
e = 1
e = 1
e = 1
e = 1
e = Inf
e = 2
e = 2
e = 1
e = 1
e = 1
e = 1
e = 2
e = 2
e = 1
e = 6
e = 2
e = 1
e = 1
e = 6
e = 2
e = 1
e = 6
e = 2
e = 6

채택된 답변

SAI SRUJAN
SAI SRUJAN 2022년 7월 22일
편집: SAI SRUJAN 2022년 7월 22일
Hi Athira T Das,
From my understanding of your question,the value of e is Inf in some cases and you want to avoid that using break statements.We can see that the value of e is inf because you are checking for a different condition in the if statement.Check for the comments in the following code snippet for better understanding,
clc; clear all; close all;
M=3;
for m1=0:M
m1;
for s1=0:1/2:m1/2
s1;
for s2=0:1/2:(M-m1)/2
s2;
if (m1-(2*s2)) < 0
break
end
for j1=0:m1-2*s1
j1;
if gamma(1+m1-(2*s2-j1))== nan;
break
end
% Here you are checking for gamma(1+m1-2*s2+j1).
if gamma(1+m1-(2*s2-j1))== Inf;
break
end
if gamma(1+m1-(2*s2-j1)) == 0
break
end
for j2=0:(M-m1-(2*s2))
j2;
q=factorial(m1-(2.*s2));
w=factorial(j1);
% Whereas here you are assigning gamma(1+m1-2*s2-j1.)
% We can see that sign of j1 is different.
% Change this to avoid assigning inf to e.
e=gamma(1+m1-(2.*s2)-j1);
end
end
end
end
end
% Consider using isnan and isinf
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 7월 22일
isnan() and isinf() can be combined
if ~isfinite(gamma(1+m1-(2*s2-j1))); break; end

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 7월 22일
if gamma(1+m1-(2*s2-j1))== nan;
That will never be true. Observe:
x = nan
x = NaN
x == x
ans = logical
0
x ~= x
ans = logical
1
You need to use isnan()

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by