How can I maintain to complex type when copying arrays?

6 views (last 30 days)
Eddie on 8 Aug 2012
Matlab 2011b
Under certain situations the complex part of my array is zero, but I still need to know that it is a complex array. I still use isreal() to determine if the array is a complex type, but if I copy the array to another array, isreal() returns 1. (see my session below).
Is this a bug?
K>> ar = 1:5;
K>> ai = zeros(1, 5);
K>> a = complex(ar,ai)
a = 1 2 3 4 5
K>> isreal(a)
ans = 0
K>> b = a(1:5);
b = 1 2 3 4 5
K>> isreal(b)
ans = 1

Accepted Answer

James Tursa
James Tursa on 8 Aug 2012
Edited: James Tursa on 8 Aug 2012
MATLAB variables store the real and imaginary parts in different areas of memory. For a real variable, the imaginary part is not physically present (the pointer to it is NULL in C terminology). The isreal function in MATLAB returns false if the imaginary part of the variable is physically present (regardless of whether it contains all 0's or not). All of the MATLAB functions seem to check each result to see if the imaginary part is all 0's, and if it is then it is physically deleted, resulting in a real variable. There is no automatic way around this that I am aware of ... you will have to force a result to be complex manually by appending a 0 complex part (e.g., via the complex function) if you want the downstream stuff to see it as complex.
I should add that there are a host of operations that you can do in MATLAB that will return a "shared data copy" of the input. Examples of these are:
B = A; % Whole array assignment
A = 1:5; B = A.'; % Transpose of a vector
A = rand(2,3); B = reshape(A,3,2); % Reshape an array
For shared data copies, the imaginary part is physically retained even if it is all 0's. Basically, most operations that do not change the data or its arrangement in memory often return a shared data copy.

More Answers (3)

Azzi Abdelmalek
Azzi Abdelmalek on 8 Aug 2012
Edited: Azzi Abdelmalek on 8 Aug 2012
instead b=a(1:5) use

Matt Fig
Matt Fig on 8 Aug 2012
Edited: Matt Fig on 8 Aug 2012
This is documented behavior (not a bug), at least in r2007b. From the documentation for ISREAL, under remarks:
"If B is real and A = complex(B), then A is a complex matrix and isreal(A) returns false, while A(m:n) returns a real matrix and isreal(A(m:n)) returns true."
I agree that this is not intuitive. I don't know why TMW would do it that way (save memory??).
It is also documented in r2012a, under Tips:
May I suggest using this instead of zeros if the issue is critical:
ar = 1:5;
ai = (eps/2)*ones(1, 5);
a = complex( ar, ai );
b = a(1:5);

Eddie on 8 Aug 2012
Thanks folks. Your answers have been a great help.
>> ar = 1:5; >> ai = zeros(1, 5)
ai = 0 0 0 0 0
>> a = complex(ar,ai)
a = 1 2 3 4 5
>> isreal(a)
ans = 0
>> b=a % This works, but I also want to trim the array.
b = 1 2 3 4 5
>> isreal(b)
ans = 0
>> b=a(1:2) % this assignment is seems to delete the imaginary part
b = 1 2
>> isreal(b) % and it consequently says its real
ans = 1
>> b=complex(a(1:2)) % but this works! Yeah!
b = 1 2
>> isreal(b)
ans = 0

Community Treasure Hunt

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

Start Hunting!