isequal - Determine array equality - MATLAB (original) (raw)

Syntax

Description

tf = isequal([A,B](#bt28kdt-AB)) returns logical1 (true) if A andB are equivalent; otherwise, it returns logical0 (false). See the Input Arguments section for a definition of equivalence for each data type. NaN (Not a Number),NaT (Not a Time), undefined categorical elements, and<missing> values are considered to be_unequal_ to other elements, as well as themselves.

To treat NaN, NaT,<undefined>, and <missing> values as equal to other such values, use isequaln.

example

tf = isequal([A1,A2,...,An](#bt28kdt-A1A2An)) returns logical1 (true) if all the inputs are equivalent.

example

Examples

collapse all

Create two numeric matrices and compare them for equality.

A = zeros(3,3)+1e-20; B = zeros(3,3); tf = isequal(A,B)

The function returns logical 0 (false) because the matrices differ by a very small amount and are not exactly equal.

Create two structures and specify the fields in a different order.

A = struct('field1',0.005,'field2',2500); B = struct('field2',2500,'field1',0.005);

Compare the structures for equality.

Even though the ordering of the fields in each structure is different, isequal treats them as the same because the values are equal.

Compare the logical value true to the double integer 1.

Notice that isequal does not consider data type when it tests for equality.

Similarly, compare 'A' to the ASCII-equivalent integer, 65.

The result is logical 1 (true) since double('A') equals 65.

Create three vectors containing NaN values.

A1 = [1 NaN NaN]; A2 = [1 NaN NaN]; A3 = [1 NaN NaN];

Compare the vectors for equality.

The result is logical 0 (false) because isequal does not treat NaN values as equal to each other.

Determine if midnight on January 13, 2013, in Anchorage, Alaska, is equal to 11 AM on the same date in Cairo.

t1 = datetime(2013,1,13,0,0,0,'TimeZone','America/Anchorage'); t2 = datetime(2013,1,13,11,0,0,'TimeZone','Africa/Cairo'); tf = isequal(t1,t2)

Add 8 months to the date, and compare the datetime values for equality.

t1 = datetime(2013,9,13,0,0,0,'TimeZone','America/Anchorage'); t2 = datetime(2013,9,13,11,0,0,'TimeZone','Africa/Cairo'); tf = isequal(t1,t2)

The datetime values are no longer equal since Cairo does not observe daylight saving time.

Even though the sizes and data types are different, isequal returns logical 1 (true) when comparing a character vector and string scalar that contain the same sequence of characters.

Input Arguments

collapse all

Inputs to be compared, specified as arrays.

In some cases, the types of the inputs do not have to match:

Some data type comparisons have special considerations involving metadata. If the inputs are all:

Series of inputs to be compared, specified as arrays.

In some cases, the types of the inputs do not have to match:

Some data type comparisons have special considerations involving metadata. If the inputs are all:

Tips

Extended Capabilities

expand all

The isequal 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).

Version History

Introduced before R2006a

expand all

In previous releases, isequal considered two or more empty object arrays equal when they were the same size, but it did not take class into account. Starting in R2022b, isequal returns logical1 (true) for empty object arrays only when the arrays have the same size and class.