why am i getting complex values when I should be getting reals?

I was doing some statistics for model performance where I was looping through large arrays with many NaN values. While performing log(value) in the loop matlab spit out complex numbers for a short spurt for no apparent reason. All the values should have been real or NaN and it was giving me NaN+NaNi or complex values of reals such as 0.0013+0.0004i. It did this for two arrays only and only for short bursts and then went back to behaving. Does anyone know why this occurs? or how to stop it from happening? Thank you.

댓글 수: 2

jgg
jgg 2016년 1월 28일
편집: jgg 2016년 1월 28일
Are you sure there were no negative values in your loop? Log will return complex values if there are negative terms.
Andre
Andre 2016년 1월 28일
편집: Andre 2016년 1월 28일
There is one but this behavior starts some 30 values prior to that and on a NaN. When I remove the negative number it stops but it still is odd to me that the complex numbers appear before they should.

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

답변 (1개)

Star Strider
Star Strider 2016년 1월 28일

0 개 추천

The log of negative values will be complex. The ‘NaN + NaNi’ values are due to the vector (or array) being complex for other values.
You can prevent the complex values by taking log(abs(x)). You have to decide if that is correct for your code.

댓글 수: 1

We need to see your code.
How do you get from ‘A’ to ‘B’? If I understand your Comment correctly, ‘A’ has 3422 elements, so you’re doing something other than reshaping it to get ‘B’.

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2016년 1월 28일

댓글:

2016년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by