How to find complex numbers in an array and turn it into something else?

22 ビュー (過去 30 日間)
Tin Nguyen
Tin Nguyen 2015 年 10 月 13 日
コメント済み: Star Strider 2015 年 10 月 13 日
So if I had an array that contains real numbers and complex number such as this:
r =
0.2876 + 0.3069i
0.2876 - 0.3069i
0.3127 + 0.0000i
And I wanted to change any complex numbers to 0?

採用された回答

Star Strider
Star Strider 2015 年 10 月 13 日
This works:
r = [0.2876 + 0.3069i
0.2876 - 0.3069i
0.3127 + 0.0000i];
r(imag(r) ~= 0) = 0
r =
0.0000e+000
0.0000e+000
312.7000e-003
Test for the imaginary part to be not equal to zero to get only the real values. Then set the complex values to zero.
  2 件のコメント
Tin Nguyen
Tin Nguyen 2015 年 10 月 13 日
Thank you very much. How about if I want to make it NaN?
Star Strider
Star Strider 2015 年 10 月 13 日
My pleasure.
To replace them with NaN, the assignment becomes:
r(imag(r) ~= 0) = NaN
r =
NaN
NaN
312.7000e-003

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by