Compare matrix matlab

Compare matrix matlab DEFAULT

isequal

Main Content

Description

example

returns logical () if and are equivalent; otherwise, it returns logical (). See the Input Arguments section for a definition of equivalence for each data type. (Not a Number), (Not a Time), undefined categorical elements, and values are considered to be unequal to other elements, as well as themselves.

To treat , , , and values as equal to other such values, use .

Examples

collapse all

Compare Two Numeric Matrices

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 () because the matrices differ by a very small amount and are not exactly equal.

Compare Two Structures

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, treats them as the same because the values are equal.

Comparing Numeric Values with Special Nonnumeric Values

Compare the logical value to the double integer .

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

Similarly, compare to the ASCII-equivalent integer, .

The result is logical () since equals .

Compare Vectors Containing NaN Values

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 () because does not treat NaN values as equal to each other.

Compare Two Datetime Values

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.

Compare Character Vector to String Scalar

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

Input Arguments

collapse all

— Inputs to be compared (as separate arguments)
arrays

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 both real and imaginary parts of numeric arrays.

  • 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.

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 compares the category names of each pair of elements.

  • Datetime arrays — ignores display format when it compares points in time. If the arrays are all associated with time zones, then compares the instants in time rather than the clockface times (for example, is the same instant as , so returns even though the clockface times of 9:00 and 6:00 differ).

  • Objects — returns logical () for objects of the same class with equal property values.

— Series of inputs to be compared (as separate arguments)
arrays

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 both real and imaginary parts of numeric arrays.

  • 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.

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 compares the category names of each pair of elements.

  • Datetime arrays — ignores display format when it compares points in time. If the arrays are all associated with time zones, then compares the instants in time rather than the clockface times (for example, is the same instant as , so returns even though the clockface times of 9:00 and 6:00 differ).

  • Objects — returns logical () 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.

  • returns logical () for two objects with dynamic properties, even if the properties have the same names and values.

  • 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. Use to determine if two objects with different handles have equal property values.

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™.

Thread-Based Environment
Run code in the background using MATLAB® or accelerate code with Parallel Computing Toolbox™ .

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™.

This function fully supports GPU arrays. 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).

Introduced before R2006a

You have a modified version of this example. Do you want to open this example with your edits?

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.

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: .

Select web site

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.

Contact your local office

Sours: https://www.mathworks.com/help/matlab/ref/isequal.html

eq, ==

Main Content

Description

example

returns a logical array with elements set to logical () where arrays and are equal; otherwise, the element is logical (). The test compares both real and imaginary parts of numeric arrays. returns logical () where or have NaN or undefined elements.

is an alternative way to execute , but is rarely used. It enables operator overloading for classes.

Examples

collapse all

Equality of Two Vectors

Create two vectors containing both real and imaginary numbers, then compare the vectors for equality.

A = [1+i 3 2 4+i]; B = [1 3+i 2 4+i]; A == B
ans = 1x4 logical array 0 0 1 1

The function tests both real and imaginary parts for equality, and returns logical () only where both parts are equal.

Find Characters

Create a character vector.

Test for the presence of a specific character using .

ans = 1x11 logical array 0 0 0 0 1 0 0 0 1 0 1

The value of logical () indicates the presence of the character .

Find Values in Categorical Array

Create a categorical array with two values: and .

A = categorical({'heads''heads''tails'; 'tails''heads''tails'})
A = 2x3 categorical heads heads tails tails heads tails

Find all values in the category.

ans = 2x3 logical array 1 1 0 0 1 0

A value of logical () indicates a value in the category.

Compare the rows of for equality.

ans = 1x3 logical array 0 1 1

A value of logical () indicates where the rows have equal category values.

Compare Floating-Point Numbers

Many numbers expressed in decimal text cannot be represented exactly as binary floating numbers. This leads to small differences in results that the operator reflects.

Perform a few subtraction operations on numbers expressed in decimal and store the result in .

With exact decimal arithmetic, should be equal to exactly. Its small value is due to the nature of binary floating-point arithmetic.

Compare to for equality.

Compare floating-point numbers using a tolerance, , instead of using .

tol = eps(0.5); abs(C-0) < tol

The two numbers, and , are closer to one another than two consecutive floating-point numbers near . In many situations, may act like .

Compare Datetime Values

Compare the elements of two arrays.

Create two arrays in different time zones.

t1 = [2014,04,14,9,0,0;2014,04,14,10,0,0]; A = datetime(t1,'TimeZone','America/Los_Angeles'); A.Format = 'd-MMM-y HH:mm:ss Z'
A = 2x1 datetime 14-Apr-2014 09:00:00 -0700 14-Apr-2014 10:00:00 -0700
t2 = [2014,04,14,12,0,0;2014,04,14,12,30,0]; B = datetime(t2,'TimeZone','America/New_York'); B.Format = 'd-MMM-y HH:mm:ss Z'
B = 2x1 datetime 14-Apr-2014 12:00:00 -0400 14-Apr-2014 12:30:00 -0400

Check where elements in and are equal.

ans = 2x1 logical array 1 0

Input Arguments

collapse all

, — Operands
scalars | vectors | matrices | multidimensional arrays

Operands, specified as scalars, vectors, matrices, or multidimensional arrays. Inputs and must either be the same size or have sizes that are compatible (for example, is an -by- matrix and is a scalar or -by- row vector). For more information, see Compatible Array Sizes for Basic Operations.

You can compare numeric inputs of any type, and the comparison does not suffer loss of precision due to type conversion.

  • If one input is a array, the other input can be a array, a cell array of character vectors, or a single character vector. A single character vector expands into a cell array of character vectors of the same size as the other input. If both inputs are ordinal arrays, they must have the same sets of categories, including their order. If both inputs are arrays that are not ordinal, they can have different sets of categories. See Compare Categorical Array Elements for more details.

  • If one input is a array, the other input can be a array, a character vector, or a cell array of character vectors.

  • If one input is a array, the other input can be a array or a numeric array. The operator treats each numeric value as a number of standard 24-hour days.

  • If one input is a string array, the other input can be a string array, a character vector, or a cell array of character vectors. The corresponding elements of and are compared lexicographically.

Data Types: | | | | | | | | | | | | | | |
Complex Number Support: Yes

Tips

  • When comparing handle objects, use to test whether objects have the same handle. Use to determine if objects with different handles have equal property values.

Compatibility Considerations

expand all

Implicit expansion change affects arguments for operators

Behavior changed in R2016b

Starting in R2016b with the addition of implicit expansion, some combinations of arguments for basic operations that previously returned errors now produce results. For example, you previously could not add a row and a column vector, but those operands are now valid for addition. In other words, an expression like previously returned a size mismatch error, but now it executes.

If your code uses element-wise operators and relies on the errors that MATLAB® previously returned for mismatched sizes, particularly within a / block, then your code might no longer catch those errors.

For more information on the required input sizes for basic array operations, see Compatible Array Sizes for Basic Operations.

Implicit expansion change affects , , and arrays

Behavior changed in R2020b

Starting in R2020b, supports implicit expansion when the arguments are , , or arrays. Between R2020a and R2016b, implicit expansion was supported only for numeric and string data types.

Extended Capabilities

Tall Arrays
Calculate with arrays that have more rows than fit in memory.

This function fully supports tall arrays. For more information, see Tall Arrays.

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

Usage notes and limitations:

  • Code generation does not support using to test equality between an enumeration member and a string array, a character array, or a cell array of character arrays.

HDL Code Generation
Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.

Thread-Based Environment
Run code in the background using MATLAB® or accelerate code with Parallel Computing Toolbox™ .

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™.

This function fully supports GPU arrays. 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).

Introduced before R2006a

You have a modified version of this example. Do you want to open this example with your edits?

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.

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: .

Select web site

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.

Contact your local office

Sours: https://www.mathworks.com/help/matlab/ref/eq.html
  1. Immigration services officer requirements
  2. Rural king rake
  3. Tv shows sidereel
  4. Lowes mowers

Select a Web Site

Array Comparison with Relational Operators

Relational operators compare operands quantitatively, using operators like “less than”, “greater than”, and “not equal to.” The result of a relational comparison is a logical array indicating the locations where the relation is true.

These are the relational operators in MATLAB®.

SymbolFunction EquivalentDescription

Less than

Less than or equal to

Greater than

Greater than or equal to

Equal to

Not equal to

Array Comparison

Numeric Arrays

The relational operators perform element-wise comparisons between two arrays. The arrays must have compatible sizes to facilitate the operation. Arrays with compatible sizes are implicitly expanded to be the same size during execution of the calculation. In the simplest cases, the two operands are arrays of the same size, or one is a scalar. For more information, see Compatible Array Sizes for Basic Operations.

For example, if you compare two matrices of the same size, then the result is a logical matrix of the same size with elements indicating where the relation is true.

Similarly, you can compare one of the arrays to a scalar.

If you compare a -by- row vector to an -by- column vector, then MATLAB expands each vector into an -by- matrix before performing the comparison. The resulting matrix contains the comparison result for each combination of elements in the vectors.

Empty Arrays

The relational operators work with arrays for which any dimension has size zero, as long as both arrays have compatible sizes. This means that if one array has a dimension size of zero, then the size of the corresponding dimension in the other array must be 1 or zero, and the size of that dimension in the output is zero.

A = ones(3,0); B = ones(3,1); A == B
ans = Empty matrix: 3-by-0

However, expressions such as

return an error if is not 0-by-0 or 1-by-1. This behavior is consistent with that of all other binary operators, such as , , , , , , and so on.

To test for empty arrays, use .

Complex Numbers

  • The operators , , , and use only the real part of the operands in performing comparisons.

  • The operators and test both real and imaginary parts of the operands.

, , , and undefined Element Comparisons

  • values are equal to other values.

  • values are not equal to any other numeric value, including other values.

  • values are not equal to any other datetime value, including other values.

  • Undefined categorical elements are not equal to any other categorical value, including other undefined elements.

Logic Statements

Use relational operators in conjunction with the logical operators (AND), (OR), (XOR), and (NOT), to string together more complex logical statements.

For example, you can locate where negative elements occur in two arrays.

For more examples, see Find Array Elements That Meet a Condition.

See Also

| | | | |

Related Topics

Sours: https://www.mathworks.com/help/matlab/matlab_prog/array-comparison-with-relational-operators.html
MATLAB - Tutorial 7 - Difference between Vectors,Matrices \u0026 Arrays

.

Matlab compare matrix

.

MATLAB - Tutorial 7 - Difference between Vectors,Matrices \u0026 Arrays

.

Now discussing:

.



2652 2653 2654 2655 2656