What does the ismember function do in MATLAB / RunMat?
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 does the ismember function behave in MATLAB / 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.
ismember Function GPU Execution Behaviour
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 residency in RunMat (Do I need gpuArray?)
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 of using the ismember function in MATLAB / RunMat
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 (RunMat gathers to host unless a provider implements ismember):
tf =
0 1 0 1
loc =
0 1 0 1
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.
See Also
unique, intersect, setdiff, union, gpuArray, gather
Source & Feedback
- Source code:
crates/runmat-runtime/src/builtins/array/sorting_sets/ismember.rs - Found a bug? Open an issue with details and a minimal repro.