필터 지우기
필터 지우기

Automatic double to complex double ?

조회 수: 86 (최근 30일)
Miroslav Mitev
Miroslav Mitev 2018년 3월 6일
댓글: Guillaume 2018년 3월 7일
I have the following vector:
vector =[ 3 2 1 -1 -2 -3 ];
And I would like to take the log of it. When I am try to do that instead of giving me complex values only for the last three it transform all the values of the new vector by adding +0.0000i:
log2(vector)
ans =
1.5850 + 0.0000i 1.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 4.5324i 1.0000 + 4.5324i 1.5850 + 4.5324i
How can I fix that?

채택된 답변

Stephen23
Stephen23 2018년 3월 6일
편집: Stephen23 2018년 3월 6일
You can't. There is no way to only store imaginary values for some elements of a floating point numeric array.
You can test if the imaginary parts are zero:
imag(M)==0

추가 답변 (1개)

Guillaume
Guillaume 2018년 3월 6일
편집: Guillaume 2018년 3월 6일
A real value is a complex value with imaginary part of 0. The first 3 elements of your output are real values, not complex.
When matlab displays a matrix where at least one element has a non-zero imaginary part, then all the values are displayed in a complex form. There is no way to turn that off. Why does it matter anyway? This is just a display.
  댓글 수: 2
James Tursa
James Tursa 2018년 3월 6일
편집: James Tursa 2018년 3월 6일
"... This is just a display..."
Well, not really. Those 0's for the real elements are physically present in memory when the variable is complex. For real variables, the imaginary 0's are not physically present and do not use memory. E.g.,
>> x = rand(1000000,1);
>> y = rand(1000000,1);
>> y(1) = 1i;
>> whos
Name Size Bytes Class Attributes
x 1000000x1 8000000 double
y 1000000x1 16000000 double complex
The Bytes used shown above is an accurate reflection of the fact that there are 999999 double 0's physically stored in memory for the y variable imaginary part in addition to the one complex element, but there is no such imaginary storage used for the x variable. So, behind the scenes there is a memory usage difference ... it is not just a display difference.
The isreal( ) function doesn't test if a variable is real valued btw ... what it really does (no pun intended) is test to see if there is an imaginary part of a variable physically stored in memory regardless of whether these imaginary values are all 0's or not. E.g.,
>> x = 0;
>> y = complex(0,0);
>> isreal(x)
ans =
1
>> isreal(y)
ans =
0
>> x==y
ans =
1
For the y variable above, we forced the imaginary 0 part to be physically stored in memory because of how we created it, hence isreal(y) returns false. Even though mathematically y is equivalent in value to the x variable.
Most native MATLAB functions check results to see if the imaginary part is all physical 0's, and if that is the case MATLAB will unattach that memory from the variable and free it. But there are exceptions to this behavior (like the complex( ) function used above).
Guillaume
Guillaume 2018년 3월 7일
Yes, I worded that badly. What I meant is that whether you write a or a + 0i, it makes no difference mathematically.
At the programmatic level, there are indeed some inevitable differences but these are not something the OP should be worried about.

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

카테고리

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