isequaln - Determine array equality, treating NaN values as equal - MATLAB (original) (raw)
Determine array equality, treating NaN values as equal
Syntax
Description
tf = isequaln([A,B](#btbbzj7-1-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_equal_ to other such values.
To treat NaN
, NaT
,<undefined>
, and <missing>
values as unequal to other such values, use isequal.
tf = isequaln([A1,A2,...,An](#btbbzj7-1-A1A2An))
returns logical1
(true
) if all the inputs are equivalent.
Examples
Create two numeric matrices and compare them for equality.
A = zeros(3,3)+1e-20; B = zeros(3,3); tf = isequaln(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, isequaln
treats them as the same because the values are equal.
Compare the logical value true
to the double integer 1
.
Notice that isequaln
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 1
(true
) because isequaln
treats the NaN values as equal to each other.
Even though the sizes and data types are different, isequaln
returns logical 1
(true
) when comparing a character vector and string scalar that contain the same sequence of characters.
Input Arguments
Inputs to be compared, specified as arrays.
In some cases, the types of the inputs do not have to match:
- Numeric inputs are equivalent if they are the same size and their contents are of equal value. The test compares real and imaginary parts of numeric arrays separately.
- Tables, timetables, structures, and cell arrays are equivalent only when all elements and properties are equal.
- String scalars and character vectors containing the same sequence of characters are equivalent.
- A categorical value is equivalent to a string scalar or character vector that names the same category.
- A datetime value is equivalent to a string scalar or character vector that represents the same point in time.
- A duration value is equivalent to a string scalar or character vector that represents the same length of time.
Some data type comparisons have special considerations involving metadata. If the inputs are all:
- Structures — Fields need not be in the same order as long as the contents are equal.
- Ordinal categorical arrays — Must have the same sets of categories, including their order.
- Categorical arrays that are not ordinal — Can have different sets of categories, and
isequaln
compares the category names of each pair of elements. - Datetime arrays —
isequaln
ignores display format when it compares points in time. If the arrays are all associated with time zones, thenisequaln
compares the instants in time rather than the clockface times (for example,01-May-2018 09:00:00 EDT
is the same instant as01-May-2018 06:00:00 PDT
, soisequaln
returnstrue
even though the clockface times of 9:00 and 6:00 differ). - Objects —
isequaln
returns logical1
(true
) for objects of the same class with equal property values.
Series of inputs to be compared, specified as arrays.
In some cases, the types of the inputs do not have to match:
- Numeric inputs are equivalent if they are the same size and their contents are of equal value. The test compares real and imaginary parts of numeric arrays separately.
- Tables, timetables, structures, and cell arrays are equivalent only when all elements and properties are equal.
- String scalars and character vectors containing the same sequence of characters are equivalent.
- A categorical value is equivalent to a string scalar or character vector that names the same category.
- A datetime value is equivalent to a string scalar or character vector that represents the same point in time.
- A duration value is equivalent to a string scalar or character vector that represents the same length of time.
Some data type comparisons have special considerations involving metadata. If the inputs are all:
- Structures — Fields need not be in the same order as long as the contents are equal.
- Ordinal categorical arrays — Must have the same sets of categories, including their order.
- Categorical arrays that are not ordinal — Can have different sets of categories, and
isequaln
compares the category names of each pair of elements. - Datetime arrays —
isequaln
ignores display format when it compares points in time. If the arrays are all associated with time zones, thenisequaln
compares the instants in time rather than the clockface times (for example,01-May-2018 09:00:00 EDT
is the same instant as01-May-2018 06:00:00 PDT
, soisequaln
returnstrue
even though the clockface times of 9:00 and 6:00 differ). - Objects —
isequaln
returns logical1
(true
) for objects of the same class with equal property values.
Tips
- The equality of two function handles depends on how they are constructed. For more information, see Compare Function Handles.
isequaln
returns logical0
(false
) for two objects with dynamic properties, even if the properties have the same names and values.isequaln
compares only stored (non-dependent) properties when testing two objects for equality.- When comparing two handle objects, use
==
to test whether objects have the same handle. Useisequaln
to determine if two objects with different handles have equal property values.
Extended Capabilities
The isequaln
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 in R2012a
In previous releases, isequaln
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, isequaln
returns logical1
(true
) for empty object arrays only when the arrays have the same size and class.