Determine whether array uses complex storage (2024)

Determine whether array uses complex storage

collapse all in page

Syntax

tf = isreal(A)

Description

example

tf = isreal(A) returns logical 1 (true) when numeric array A does not have an imaginary part, and logical 0 (false) otherwise. isreal returns logical 0 (false) for complex values that have zero imaginary part, since the value is still stored as a complex number.

Examples

collapse all

Determine Whether Matrix Contains All Real Values

Open Live Script

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

Open Live Script

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

Open Live Script

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

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

Open Live Script

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]} {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:6x(k,1) = isreal(C{k,1});endx
x = 6x1 logical array 1 1 0 1 1 0

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

Input Arguments

collapse all

AInput array
scalar | vector | matrix | multidimensional array

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

  • For numeric data types, if A doesnot have an imaginary part, isreal returns true;if A does have an imaginary part isreal returns false.

  • For duration, calendarDuration, logical, and char data types, isreal always returns true.

  • For string, table, cell, struct, datetime, function_handle, and object data types, isreal always returns false.

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, use A == real(A).

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

  • ~isreal(x) detects arrays thathave 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™.

This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a

See Also

complex | isnumeric | isnan | isprime | isfinite | isinf | isa

Topics

  • Create Complex Numbers

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Determine whether array uses complex storage (1)

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:

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
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Determine whether array uses complex storage (2024)
Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5844

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.