필터 지우기
필터 지우기

Why am I getting an NaN value for my array?

조회 수: 2 (최근 30일)
Greylyn Gao
Greylyn Gao 2021년 11월 18일
답변: Walter Roberson 2021년 11월 18일
n5 = [-5:5];
y0 = abs(Dn2f(0))
y0 = 0.5000
ys = abs(Dn2f(n5))
ys = 1×11
0.0900 0.0000 0.1501 0.3183 0.4502 NaN 0.4502 0.3183 0.1501 0.0000 0.0900
function Dn = Dn2f(n)
if n == 0
Dn = 1/2;
else
Dn = (1-exp(-1j*pi*n/2))./(1j*pi*n);
end
end
I specifically defined the value of my function at 0 because of a division by 0, and it even returns the correct value at 0 when I call it for a single value, but in an array, the value at 0 is NaN?

답변 (1개)

Walter Roberson
Walter Roberson 2021년 11월 18일
In MATLAB if you test a non-scalar in an "if" statement, then the statement is considered true only if all of the outputs of the expression are non-zero. "if" does not act as a selector to only work on the elements that the test is true for.
You should learn how to use logical indexing.

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by