RunMat
GitHub

ismember — Test membership of elements or rows in MATLAB and RunMat.

ismember(A, B) returns membership flags for elements (or rows) of A in B, with optional first-match indices. Ordering and comparison behavior follow MATLAB semantics.

Syntax

tf = ismember(A, B)
tf = ismember(A, B, option...)
[tf, loc] = ismember(A, B)
[tf, loc] = ismember(A, B, option...)

Inputs

NameTypeRequiredDefaultDescription
AAnyYesValues or rows to query.
BAnyYesReference set of values or rows.
optionStringScalarVariadicOption tokens: 'rows'.

Returns

NameTypeDescription
tfLogicalArrayMembership mask over A.
locNumericArrayFirst-match indices into B for each element/row in A (0 when absent).

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

Errors

IdentifierWhenMessage
RunMat:ismember:LegacyOptionUnsupportedLegacy compatibility options are requested.ismember: the 'legacy' behaviour is not supported
RunMat:ismember:UnknownOptionAn unsupported option token is provided.ismember: unrecognised option
RunMat:ismember:RowsColumnMismatch'rows' mode is used and column counts differ.ismember: inputs must have the same number of columns when using 'rows'

How ismember works

  • The first output tf has the same shape as A (or size(A,1) × 1 when using 'rows').
  • The optional second output loc contains one-based indices into B, with 0 for values that are not found.
  • Duplicate values in A return the index of the first occurrence in B every time they match.
  • NaN values are treated as identical so they match other NaN entries in B.
  • Character arrays follow column-major linear indexing, mirroring MATLAB.
  • The 'rows' option compares complete rows; inputs must agree on the number of columns.
  • Legacy flags ('legacy', 'R2012a') are deliberately unsupported in RunMat.

Does RunMat run ismember on the GPU?

When either input is a GPU tensor, RunMat first checks whether the active acceleration provider exposes a custom ismember hook. Until providers implement that hook, the runtime transparently gathers GPU operands to host memory, performs the membership lookup using the CPU implementation, and returns host-resident outputs so results exactly match MATLAB.

GPU memory and residency

Most code does not need to call gpuArray explicitly. The native auto-offload planner keeps track of residency and recognises that ismember is a sink: the operation produces logical outputs and one-based indices that currently live on the host. If an acceleration provider exposes a full ismember hook in the future, the planner can keep data on the device automatically. Until then, manual gpuArray / gather calls only serve to mirror MATLAB workflows; RunMat already performs the necessary transfers when it detects that tensors reside on the GPU.

Examples

Checking membership of numeric vectors

A = [5 7 2 7];
B = [7 9 5];
[tf, loc] = ismember(A, B)

Expected output:

tf =
     1     1     0     1
loc =
     3     1     0     1

Finding row membership in a matrix

A = [1 2; 3 4; 1 2];
B = [3 4; 5 6; 1 2];
[tf, loc] = ismember(A, B, 'rows')

Expected output:

tf =
     1
     1
     1
loc =
     3
     1
     3

Locating values and retrieving the index

values = [10 20 30];
set = [30 10 40];
[tf, loc] = ismember(values, set)

Expected output:

tf =
     1     0     1
loc =
     2     0     1

Testing characters against a set

chars = ['r','u'; 'n','m'];
set = ['m','a'; 'r','u'];
[tf, loc] = ismember(chars, set)

Expected output:

tf =
     1     1
     0     0
loc =
     3     1
     0     0

Working with string arrays

A = ["apple" "pear" "banana"];
B = ["pear" "orange" "apple"];
[tf, loc] = ismember(A, B)

Expected output:

tf =
  1×3 logical array
   1   1   0
loc =
  1×3 double
   3   1   0

Using ismember with gpuArray inputs

G = gpuArray([1 4 2 4]);
H = gpuArray([4 5]);
[tf, loc] = ismember(G, H)

Expected output:

tf =
     0     1     0     1
loc =
     0     1     0     1

Using ismember with coding agents

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

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

FAQ

Does ismember treat NaN values as equal?

Yes. NaN values compare equal for membership tests so every NaN in A matches any NaN in B.

What happens when an element of A is not found in B?

The corresponding logical entry is false and the index output stores 0, matching MATLAB.

Can I use ismember with string arrays and character arrays?

Yes. String arrays, scalar strings, and character arrays are supported. Mixed string/char inputs should be normalised (for example, convert scalars with string).

How does the 'rows' option change the output shape?

'rows' compares entire rows and returns outputs of size size(A,1) × 1, regardless of how many columns the input matrices contain.

Are the legacy flags supported?

No. RunMat only implements modern MATLAB semantics. Passing 'legacy' or 'R2012a' raises an error, just like other set builtins in RunMat.

Will ismember run on the GPU automatically?

If the active provider advertises an ismember hook, the runtime can keep tensors on the device. Otherwise the data is gathered to the host with no behavioural differences.

Sorting Sets

argsort · intersect · issorted · setdiff · sort · sortrows · union · unique

Shape

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

Creation

colon · eye · false · fill · linspace · logspace · magic · meshgrid · ones · peaks · rand · randi · randn · randperm · range · true · zeros

Indexing

find · ind2sub · sub2ind

Introspection

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

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ismember 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.