Main Content

isequal

Determine if symbolic inputs are equal

Description

example

tf = isequal(A,B) returns logical 1 (true) if A and B are the same size and their contents are of equal value (from a coding perspective). Otherwise, isequal returns logical 0 (false). isequal does not consider NaN (Not a Number) values to be equal. isequal recursively compares the contents of symbolic data structures and the properties of objects. If all contents in the corresponding locations are equal, isequal returns logical 1 (true).

example

tf = isequal(A1,A2,...,An) returns logical 1 (true) if all the inputs A1,A2,...,An are equal.

Examples

collapse all

Test numeric or symbolic inputs for equality using isequal. If you compare numeric inputs against symbolic inputs, isequal returns 0 (false) because double and sym are distinct data types.

Test if 2 and 5 are equal. Because you are comparing doubles, the MATLAB® isequal function is called. isequal returns 0 (false) as expected.

tf = isequal(2,5)
tf = logical
   0

Test if the solution of the equation cos(x) == -1 is pi. The isequal function returns 1 (true), meaning the solution is equal to pi.

syms x
sol = solve(cos(x) == -1,x);
tf = isequal(sol,sym(pi))
tf = logical
   1

Compare the double and symbolic representations of pi. isequal returns 0 (false) because double and sym are distinct data types. To return 1 (true) in this case, use logical instead.

usingIsEqual = isequal(pi,sym(pi))
usingIsEqual = logical
   0

usingLogical = logical(pi == sym(pi))
usingLogical = logical
   1

Test if the expressions tan(x) and sin(x)/cos(x) are equal (from a coding perspective). The isequal function returns 0 (false) because the expressions are different. isequal does not mathematically compare the expressions.

syms x
tf = isequal(tan(x),sin(x)/cos(x))
tf = logical
   0

Rewrite the expression tan(x) in terms of sin(x) and cos(x). Test if rewrite correctly rewrites tan(x) as sin(x)/cos(x). The isequal function returns 1 (true).

f = rewrite(tan(x),"sincos");
testf = sin(x)/cos(x);
tf = isequal(f,testf)
tf = logical
   1

To check if the mathematical comparison tan(x) == sin(x)/cos(x) holds true for all values of x, use isAlways.

tf = isAlways(tan(x) == sin(x)/cos(x))
tf = logical
   1

Test if solutions of the quadratic equation found by solve are equal to the expected solutions. The isequal function returns 1 (true), meaning the symbolic vectors are equal.

syms a b c x
eqn = a*x^2 + b*x + c;
Sol = solve(eqn,x);
testSol = [-(b+(b^2-4*a*c)^(1/2))/(2*a); -(b-(b^2-4*a*c)^(1/2))/(2*a)];
tf = isequal(Sol,testSol)
tf = logical
   1

The Hilbert matrix is a special matrix that is difficult to invert accurately. If the inverse is accurately computed, then multiplying the inverse by the original Hilbert matrix returns the identity matrix.

Use this condition to test if the inverse of hilb(20) is correctly calculated. isequal returns 1 (true), meaning that the product of the inverse and the original Hilbert matrix is equal to the identity matrix.

H = sym(hilb(20));
prod = H*inv(H);
eye20 = sym(eye(20));
tf = isequal(prod,eye20)
tf = logical
   1

Compare three vectors containing NaN (not a number). isequal returns logical 0 (false) because isequal does not treat NaN values as equal.

syms x
A1 = [x NaN NaN];
A2 = [x NaN NaN];
A3 = [x NaN NaN];
tf = isequal(A1,A2,A3)
tf = logical
   0

Input Arguments

collapse all

Inputs to compare, specified as numbers, vectors, matrices, or arrays, or symbolic numbers, scalar variables, matrix variables, arrays, functions, matrix functions, or expressions.

Series of inputs to compare, specified as numbers, vectors, matrices, or arrays, or symbolic numbers, scalar variables, matrix variables, arrays, functions, matrix functions, or expressions.

Tips

  • isequal(A,B) checks if A and B are the same size and their contents are equal (from a coding perspective). To check whether the condition A == B is always mathematically true for all values of variables in A and B, use isAlways(A == B).

  • If one of the input arguments is a symbolic type and the other input is a MATLAB® numeric type with the same value, then isequal returns logical 0 (false) because the inputs do not have the same data type. For example, tf = isequal(1,sym(1)) returns 0 (false).

Version History

Introduced before R2006a

expand all