필터 지우기
필터 지우기

Using NaN in if and interp1 commands

조회 수: 1 (최근 30일)
Volkan Yangin
Volkan Yangin 2017년 3월 25일
댓글: Volkan Yangin 2017년 3월 25일
Hi everbody, I have 6 values and some values are NaN. If interp1 command finds the final value as NaN for giving ' c' value, MATLAB must give me error message.
a=[1 2 3 4 5 6];
b=[10 15 20 NaN NaN NaN];
c=[1.5 4 3.5 4.5 5.1 5.9];
for g=1:1:numel(a)
if interp1(a,b,c(g))==NaN;
disp('There is a mistake here')
end
end
But MATLAB does't run this command with NaN values.
*In interp1 at 178
Warning: NaN found in Y, interpolation at undefined values
will result in undefined values.*
How can i achieve this problem? How can i use NaN values in interp1 and if command? I get the problems when i use isnan.
Thanks...
  댓글 수: 2
per isakson
per isakson 2017년 3월 25일
c(g) is a scalar. Why not replace
if interp1(a,b,c(g))==NaN;
by
if isnan( interp1(a,b,c(g)) )
?
Volkan Yangin
Volkan Yangin 2017년 3월 25일
Thank you per isakson. I am using the command you sent which includes isnan.

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

채택된 답변

Jan
Jan 2017년 3월 25일
편집: Jan 2017년 3월 25일
It is tzhe definition of NaN, that any comparison with it is false, even NaN == NaN. Therefore
if interp1(a,b,c(g))==NaN;
will most likely not do, what you expect. I assume you want:
for g = 1:numel(c) % Not numel(a) !??
cg = interp1(a, b, c(g));
if isnan(cg)
disp('There is a mistake here')
end
end
Or without a loop:
cg = interp1(a, b, c);
if any(isnan(cg))
disp('There is a mistake here')
end
I do not get a warning in R2016b.
  댓글 수: 1
Volkan Yangin
Volkan Yangin 2017년 3월 25일
편집: Volkan Yangin 2017년 3월 25일
Yes, it works on my MATLAB, too, but
Warning: NaN found in Y, interpolation at undefined values will result in undefined values. > In interp1 at 178
warning message is still on my command window.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by