Documentation

isreal

Determine whether array is real

Syntax

tf = isreal(A)

Description

example

tf = isreal(A)returns logical1(true) whenAdoes not have an imaginary part. Otherwise, it returns logical0(false).

IfAhas a stored imaginary part with value0, thenisreal(A)returns logical0(false).

Examples

collapse all

Define a 3-by-4 matrix,A.

A = [7 3+4i 2 5i;...2i 1+3i 12 345;...52 108 78 3];

Determine whether the array is real.

tf = isreal(A)
tf =logical0

自从Acontains complex elements,isrealreturns false.

Use thecomplexfunction to create a scalar,A, with zero-valued imaginary part.

A = complex(12)
A = 12.0000 + 0.0000i

Determine whetherA是真实的。

tf = isreal(A)
tf =logical0

A不是真正的因为it has an imaginary part, even though the value of the imaginary part is0.

Determine whetherAcontains any elements with zero-valued imaginary part.

~any(imag(A))
ans =logical1

A包含具有零值虚构部分的元素。

Define two complex scalars,xy.

x=3+4i; y=5-4i;

Determine whether the addition of two complex scalars,xy, is real.

A = x+y
A = 8

MATLAB® drops the zero imaginary part.

isreal(A)
ans =logical1

Ais real since it does not have an imaginary part.

Create a cell array.

C{1,1} = pi;% doubleC{2,1} ='John Doe';% char arrayC{3,1} = 2 + 4i;% complex doubleC{4,1} = ispc;% logicalC{5,1} = magic(3);% double arrayC{6,1} = complex(5,0)% complex double
C =6×1 cell array[ 3.1416] 'John Doe' [2.0000 + 4.0000i] [ 0] [3×3 double] [5.0000 + 0.0000i]

Cis a 1-by-6 cell array.

Loop over the elements of a cell array to distinguish between real and complex elements.

fork = 1:6 x(k,1) = isreal(C{k,1});endx
x =6×1 logical array1 1 0 1 1 0

All butC{3,1}C{6,1}are real arrays.

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, or multidimensional array.

  • For numeric data types, ifAdoes not have an imaginary part,isrealreturnstrue; ifAdoes have an imaginary partisrealreturnsfalse.

  • Forlogicalchardata types,isrealalways returnstrue.

  • Fortable,cell,struct,datetime,function_handle, andobjectdata types,isrealalways returnsfalse.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char|struct|table|cell|datetime|function_handle
Complex Number Support:Yes

Tips

  • isreal(complex(A))always returnsfalse, even when the imaginary part is all zeros.

  • ~isreal(x)detects arrays that have an imaginary part, even if it is all zeros.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced before R2006a

Was this topic helpful?