필터 지우기
필터 지우기

Anonymous function returning either NaN or a real number depending if the input is a scalar or an element of a vector

조회 수: 1 (최근 30일)
I have the following problem: I have created a function f(a,b,c), where b and c are scalars and a is a vector, a = [1 2 3 4 5]. Function f returns 4 real values and for the last element of the vector it returns NaN. What is strange, when I supply the Input 'by hand' ie. f(5,b,c) it returns a real value but when I write f(a(5),b,c) it is NaN. It doesn't matter how long vector a is but if I change the value of the last element it can help. Has anyone encounered a similar problem?
  댓글 수: 4
Steven Lord
Steven Lord 2016년 10월 12일
You should also post the result of the computation a(5)-5. My guess is that you're computing a and while the last element looks like it is exactly 5 that it is not, and is slightly larger leading to Inf or NaN values creeping into the calculation. When you pass in the exact value 5 you're right on the edge; when you pass in something just bigger than 5 you fall over.
Adam Pigon
Adam Pigon 2016년 10월 15일
Dear All, I would like to thank you for your comments. Obviously, you were right - the last element of my vector a is actually not what it seems to be in the short format. Vector a was created as something like a = sqrt(x)^2 and a is not necessarily equal to x. Thank you for this hint!

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

답변 (0개)

카테고리

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