MATLAB Answers


How can I separate complex numbers from the other in a matrix?

Asked by masoud jiryaei on 10 May 2019
Latest activity Commented on by the cyclist
on 10 May 2019
I have a matrix that includes: complex numbers , natural numbers and ...
I want to separate the complex numbers from the other
How can I do this ?
Is there any order in MATLAB ?


Sign in to comment.

1 Answer

Answer by the cyclist
on 10 May 2019
Edited by the cyclist
on 10 May 2019

% Example data
z = [1 1+2*i 3 4+5*i]
mask = imag(z)==0;
The variable mask is a logical variable that is TRUE at the locations of the numbers that do not have an imaginary component, and FALSE otherwise.
You'll need to be careful if you have imaginary parts that are within floating-point precision of zero. You might need to use a tolerance, rather than strict equality.


Show 1 older comment
But if I want to show me the complex numbers, what should I do ?
for example got them and put in a matrix?
realElements = z(mask);
imagElements = z(~mask);

Sign in to comment.