ismembertol — Test numeric membership within tolerance in MATLAB and RunMat.

ismembertol(A, B) returns logical membership flags for real numeric values in A that are within tolerance of values in B. Optional outputs return first or all matching one-based indices into B.

Syntax

LIA = ismembertol(A, B)
LIA = ismembertol(A, B, tol, Name, Value)
[LIA, LocB] = ismembertol(A, B)
[LIA, LocB] = ismembertol(A, B, tol, Name, Value)

Inputs

NameTypeRequiredDefaultDescription
ANumericArrayYesQuery values or rows.
BNumericArrayYesReference values or rows.
tolNumericScalarNo1e-12 for double, 1e-6 for singleRelative tolerance.
Name,ValueAnyVariadicName-value options: ByRows, DataScale, OutputAllIndices.

Returns

NameTypeDescription
LIALogicalArrayLogical mask over A.
LocBAnyFirst or all matching indices into B.

Returned values from ismembertol depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:ismembertol:InvalidInputA or B is not a supported real full numeric input.ismembertol: inputs must be real full numeric arrays
RunMat:ismembertol:InvalidArgumentTolerance or name-value arguments are malformed.ismembertol: invalid argument
RunMat:ismembertol:RowsColumnMismatchByRows is true and A/B column counts differ.ismembertol: inputs must have the same number of columns when ByRows is true
RunMat:ismembertol:InternalInternal conversion or allocation fails.ismembertol: internal error

How ismembertol works

  • Without an explicit tolerance, RunMat uses MATLAB-compatible defaults: 1e-12 for double inputs and 1e-6 when either input is single precision.
  • Element comparisons use abs(A-B) <= tol * max(abs([A(:); B(:)])) unless DataScale is supplied.
  • DataScale, when scalar, changes the tolerance test to abs(A-B) <= tol * DataScale.
  • ByRows, true compares complete rows and returns outputs of size size(A,1) x 1; A and B must have the same number of columns.
  • In row mode, vector DataScale supplies one scale per column. A scale of Inf ignores that column during matching.
  • The optional second output LocB contains the first one-based index in B for each match and 0 when absent.
  • OutputAllIndices, true makes LocB a cell array whose cells contain all matching one-based indices, or [0] when no match exists.
  • NaN values do not match. Exact equalities, including equal infinities, match before tolerance subtraction is considered.
  • Complex, sparse, string, character, cell, struct, and object inputs are rejected; ismembertol is limited to real full numeric inputs. RunMat also accepts logical values by promoting them to numeric 0/1 values.

Does RunMat run ismembertol on the GPU?

ismembertol is registered as a sink builtin. When either input tensor lives on the GPU the runtime gathers it to host memory and performs tolerance matching there.

After gathering, RunMat supports the same host options, including ByRows and OutputAllIndices. This is an intentional RunMat extension over MATLAB's currently narrower gpuArray option support.

The result is host-resident logical/index data. This preserves MATLAB-compatible tolerance scaling, row matching, and all-indices cell output while providers lack a dedicated kernel.

Examples

Testing approximate membership with default scaling

A = [0.1 1e10];
B = [0.1 + 1e-14, 1e10 + 5e-3];
LIA = ismembertol(A, B)

Expected output:

LIA =
  1x2 logical array
   1   1

Returning first matching indices

A = [1 2 4];
B = [2.01 2.02 4.2];
[LIA, LocB] = ismembertol(A, B, 0.05)

Expected output:

LIA =
  1x3 logical array
   0   1   1
LocB =
     0     1     3

Using absolute tolerance with DataScale

A = 1e10;
B = 1e10 + 5e-3;
LIA = ismembertol(A, B, 1e-6, 'DataScale', 1)

Expected output:

LIA =
     0

Comparing rows and ignoring one column

A = [0 0.5; 10 20];
B = [0.05 999; 10.2 -999];
[LIA, LocB] = ismembertol(A, B, 0.1, 'ByRows', true, 'DataScale', [1 Inf])

Expected output:

LIA =
  2x1 logical array
   1
   0
LocB =
     1
     0

Returning all matching locations

A = 2;
B = [1.99 2.01 9.5];
[LIA, LocB] = ismembertol(A, B, 0.01, 'OutputAllIndices', true)

Expected output:

LIA =
     1
LocB =
  1x1 cell array
    {[1; 2]}

Using gpuArray inputs

G = gpuArray([1; 2]);
H = gpuArray([1 + 1e-13; 3]);
LIA = ismembertol(G, H)

Expected output:

LIA =
  2x1 logical array
   1
   0

Using ismembertol with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how ismembertol changes the result.

Run a small ismembertol example, explain the result, then change one input and compare the output.

FAQ

How is the default tolerance chosen?

Double inputs use 1e-12. If either input is single precision, RunMat uses 1e-6.

Does ismembertol support string or cell inputs like ismember?

No. MATLAB's tolerance membership operation is numeric; RunMat accepts real full numeric and logical inputs only.

What does OutputAllIndices change?

The second output becomes a cell array. Each cell contains every one-based index in B within tolerance of the corresponding element or row of A, or [0] when no match exists.

Can DataScale be a vector?

Only when ByRows is true. Each vector element scales the tolerance for the corresponding column; Inf ignores that column.

Do NaN values match each other?

No. NaN does not match any value, including another NaN, for ismembertol.

Will ismembertol run on the GPU automatically?

GPU inputs are accepted and gathered automatically today. RunMat also supports ByRows and OutputAllIndices after gathering; that option support is a RunMat compatibility extension beyond MATLAB's current gpuArray restrictions.

Shape

blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · toeplitz · tril · triu · vertcat

Creation

colon · eye · false · fill · full · inf · linspace · logspace · magic · meshgrid · nan · nchoosek · ndgrid · nonzeros · ones · peaks · perms · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · zeros

Indexing

find · ind2sub · sub2ind

Introspection

iscolumn · isempty · ismatrix · isrow · isscalar · isvector · length · ndims · numel · size

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ismembertol is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.