RunMat
GitHub

View all functions

CategoryLogical: Rel

isequal — Test arrays for equality in size, class, and content.

isequal compares two or more arrays and returns true (logical 1) if they all have the same size, class, and content, and false (logical 0) otherwise. NaN values are NOT considered equal; use isequaln to treat NaN values as equal.

How does the isequal function behave in MATLAB / RunMat?

  • isequal(A, B) compares two values for equality.
  • isequal(A, B, C, ...) compares all inputs against each other; returns true only if all are equal.
  • Arrays must have identical shape (size) to be equal.
  • Arrays must have matching data types (class) to be equal.
  • All corresponding elements must be identical for arrays to be equal.
  • NaN values are NOT equal to each other (NaN ~= NaN).
  • Empty arrays [] are equal to other empty arrays with the same shape.
  • Cell arrays are compared element-by-element recursively.
  • Structs are compared field-by-field.

GPU behavior

When comparing gpuArray inputs, isequal gathers all tensors to the host before performing the comparison. The result is a scalar logical value on the host.

GPU residency

None. isequal gathers GPU tensors to the host for comparison. The result is always a host logical scalar.

Examples of using isequal in MATLAB / RunMat

Comparing two identical scalars

isequal(5, 5)

Expected output:

ans = logical
     1

Comparing two different scalars

isequal(5, 6)

Expected output:

ans = logical
     0

Comparing identical matrices

A = [1 2; 3 4];
B = [1 2; 3 4];
isequal(A, B)

Expected output:

ans = logical
     1

Comparing matrices with different shapes

A = [1 2 3];
B = [1; 2; 3];
isequal(A, B)

Expected output:

ans = logical
     0

Comparing multiple arrays at once

isequal([1 2], [1 2], [1 2])

Expected output:

ans = logical
     1

NaN values are not equal

isequal(NaN, NaN)

Expected output:

ans = logical
     0

Verifying that every cell starts with []

C = cell(2, 2);
isequal(C{1,1}, [], C{2,2}, [])

Expected output:

ans = logical
     1

FAQ

How is isequal different from ==?

isequal returns a single logical value indicating whether all inputs are completely identical. The == operator performs element-wise comparison and returns an array of the same size as the inputs.

How does isequal handle NaN values?

NaN values are NOT considered equal. isequal(NaN, NaN) returns false. Use isequaln if you want NaN values to be treated as equal.

Can I compare more than two arrays?

Yes. isequal(A, B, C, ...) accepts any number of inputs and returns true only if all inputs are equal to each other.

Are empty arrays equal?

Empty arrays with the same shape are equal. For example, isequal([], []) returns true, and isequal(zeros(0,3), zeros(0,3)) returns true.

How are cell arrays compared?

Cell arrays are compared element-by-element. Each corresponding pair of cells must contain equal values for the cell arrays to be equal.

What happens with different data types?

Values of different classes are generally not equal. However, compatible numeric types (like double and single, or double and logical) may be compared after appropriate promotion.

See also

eq, strcmp

Source & Feedback