MATLAB Answers


How do I test a vector element by element for imaginary elements

Asked by Asemahle Ndamase on 8 Apr 2019
Latest activity Commented on by Asemahle Ndamase on 9 Apr 2019
Here is the the vector i Have
r =
-1.6659 + 0.0000i
-0.3049 + 1.1564i
-0.3049 - 1.1564i
1.1378 + 0.3231i
1.1378 - 0.3231i
I am trying to check which elements are real numbers by means of a code, then after i'll have to calculate the percentage of those that are imaginary. Pls help
Thank You.


Sign in to comment.

1 Answer

Answer by John D'Errico
on 8 Apr 2019
Edited by John D'Errico
on 8 Apr 2019
 Accepted Answer

What does the function imag return?
Given that, what would
imag( r ) ~= 0
tell you? see that it returns a true or false value for each element.
Now, how would you compute the fraction of imaginary elements? That seems simple, since you can write things like
sum(imag( r ) ~= 0)

  1 Comment

Thank You John, that solved my problem

Sign in to comment.