How to test if each element of a matrix is not a real number?

24 ビュー (過去 30 日間)
John
John 2014 年 12 月 28 日
回答済み: Star Strider 2014 年 12 月 28 日
I am trying substitute for zero each element that is not a real number, without using loops. Here is a mwe:
clear all;
clc;
A=ones(5,5);
A(3,3)=1i;
A(~isreal(A)) = 0;
A
~isreal doesn't seem to test element-wise. That is, it tests the matrix as a whole: if there is a single element that is complex, then the whole matrix is tested as complex. Moreover, the substitution is performed only to the very first element. How can this be fixed?

採用された回答

Star Strider
Star Strider 2014 年 12 月 28 日
This works:
A=ones(5,5);
A(3,3)=1i;
A(imag(A) ~=0) = 0;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by