isreal
Determine whether array uses complex storage
Syntax
Description
Examples
Determine Whether Matrix Contains All Real Values
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 = logical
0
Since A
contains complex elements, isreal
returns false.
Define Complex Number with Zero-Valued Imaginary Part
Use the complex
function to create a scalar, A
, with zero-valued imaginary part.
A = complex(12)
A = 12.0000 + 0.0000i
Determine whether A
is real.
tf = isreal(A)
tf = logical
0
A
is not real because it has an imaginary part, even though the value of the imaginary part is 0
.
Determine whether A
contains any elements with zero-valued imaginary part.
~any(imag(A))
ans = logical
1
A
contains elements with zero-valued imaginary part.
Computation Resulting in Zero-Valued Imaginary Part
Define two complex scalars, x
and y
.
x=3+4i; y=5-4i;
Determine whether the addition of two complex scalars, x
and y
, is real.
A = x+y
A = 8
MATLAB® drops the zero imaginary part.
isreal(A)
ans = logical
1
A
is real since it does not have an imaginary part.
Find Real Elements in Cell Array
Create a cell array.
C{1,1} = pi; % double C{2,1} = 'John Doe'; % char array C{3,1} = 2 + 4i; % complex double C{4,1} = ispc; % logical C{5,1} = magic(3); % double array C{6,1} = complex(5,0) % complex double
C=6×1 cell array
{[ 3.1416]}
{'John Doe' }
{[2.0000 + 4.0000i]}
{[ 0]}
{3x3 double }
{[5.0000 + 0.0000i]}
C
is a 1-by-6 cell array.
Loop over the elements of a cell array to distinguish between real and complex elements.
for k = 1:6 x(k,1) = isreal(C{k,1}); end x
x = 6x1 logical array
1
1
0
1
1
0
All but C{3,1}
and C{6,1}
are real arrays.
Input Arguments
A
— Input array
scalar | vector | matrix | multidimensional array
Input array, specified as a scalar, vector, matrix, or multidimensional array.
For numeric data types, if
A
does not have an imaginary part,isreal
returnstrue
; ifA
does have an imaginary partisreal
returnsfalse
.For
duration
,calendarDuration
,logical
, andchar
data types,isreal
always returnstrue
.For
string
,table
,cell
,struct
,datetime
,function_handle
, andobject
data types,isreal
always returnsfalse
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
| string
| struct
| table
| cell
| datetime
| duration
| calendarDuration
| function_handle
Complex Number Support: Yes
Tips
To check whether each element of an array
A
is real, useA == real(A)
.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™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The isreal
function
fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray
(Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)