Automatic double to complex double ?

59 visualizzazioni (ultimi 30 giorni)
Miroslav Mitev
Miroslav Mitev il 6 Mar 2018
Commentato: Guillaume il 7 Mar 2018
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?

Risposta accettata

Stephen23
Stephen23 il 6 Mar 2018
Modificato: Stephen23 il 6 Mar 2018
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

Più risposte (1)

Guillaume
Guillaume il 6 Mar 2018
Modificato: Guillaume il 6 Mar 2018
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 Commenti
James Tursa
James Tursa il 6 Mar 2018
Modificato: James Tursa il 6 Mar 2018
"... 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 il 7 Mar 2018
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.

Accedi per commentare.

Categorie

Scopri di più su Performance and Memory in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by