필터 지우기
필터 지우기

why is 1+ nan*1i = NaN + NaNi ?

조회 수: 3 (최근 30일)
Patrick Mboma
Patrick Mboma 2015년 11월 27일
댓글: Patrick Mboma 2015년 11월 28일
Dear all,
I am trying to figure out how to store some information using complex numbers and retrieving that information later on.
For an operation like
1+nan*1i,
I would like to be able to retrieve "1" by doing
real(1+nan*1i)
and retrieve "nan" by doing
imag(1+nan*1i).
The first case does not work because matlab stores 1+ nan*1i as NaN + NaNi. Is there any workaround to this?
Thanks

채택된 답변

the cyclist
the cyclist 2015년 11월 27일
I don't envision any workaround that would specifically make NaNs work. Speaking mathematically very loosely, the NaN in your original expression could be a "complex NaN", such that the NaN-ness leaks over to the real part.
I can imagine, without deep thinking, a couple workarounds, that are annoying in their own way, but might work in your particular situation.
  • Use another matrix dimension to keep real and imaginary parts separate
  • Define your own complex number object, that has the properties you outline above.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 11월 27일
A = complex(1,nan)
real(A), imag(A)
B = A
However if you were to do
2*A
then you would get complex(NaN, NaN). You can use complex() to force unusual structures such as complex(5,0) but any arithmetic on it is going to demote it to complex(NaN, NaN)
  댓글 수: 1
Patrick Mboma
Patrick Mboma 2015년 11월 28일
Hi Walter,
Thanks for the reply. I did not know about the complex function. Thanks also for making me aware of it.
Your solution will surely work if I have a scalar. But if I start stacking items in a vector, I am not sure what the results will be. For now I think I will use the safe option of just creating another dimension to keep the numbers separate.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by