ismember — Identify array elements or rows that appear in another array while returning first-match indices.
ismember(A, B) compares the elements (or rows) of A against B and returns a logical array marking which members of A are present in B. The optional second output reports the index in B of the first matched element. RunMat follows MATLAB semantics for numeric, logical, complex, string, and character arrays.
How ismember works in RunMat
- The first output
tfhas the same shape asA(orsize(A,1) × 1when using'rows'). - The optional second output
loccontains one-based indices intoB, with0for values that are not found. - Duplicate values in
Areturn the index of the first occurrence inBevery time they match. NaNvalues are treated as identical so they match otherNaNentries inB.- 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.
How ismember runs 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 1Finding 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
3Locating 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 1Testing 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 0Working 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 0Using 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 1FAQ
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.
Related functions to explore
These functions work well alongside ismember. Each page has runnable examples you can try in the browser.
unique, intersect, setdiff, union, gpuArray, gather, argsort, issorted, sort, sortrows
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ismember works, line by line, in Rust.
- View ismember.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.