MATLAB Answers

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

39 views (last 30 days)
John
John on 28 Dec 2014
Answered: Star Strider on 28 Dec 2014
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?

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 28 Dec 2014
This works:
A=ones(5,5);
A(3,3)=1i;
A(imag(A) ~=0) = 0;

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags