How can I maintain to complex type when copying arrays?
Show older comments
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
More Answers (3)
Azzi Abdelmalek
on 8 Aug 2012
Edited: Azzi Abdelmalek
on 8 Aug 2012
instead b=a(1:5) use
b=a
%or
b=complex(a(1:5))
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 );
isreal(a)
b = a(1:5);
isreal(b)
Eddie
on 8 Aug 2012
0 votes
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!