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. Supported ordering and comparison forms are described below.
Syntax
tf = ismember(A, B)
tf = ismember(A, B, option...)
[tf, loc] = ismember(A, B)
[tf, loc] = ismember(A, B, option...)Inputs
A- Type
Any- Required
- Yes
- Description
- Values or rows to query.
B- Type
Any- Required
- Yes
- Description
- Reference set of values or rows.
option- Type
StringScalar- Required
- Variadic
- Description
- Option tokens: 'rows'.
Returns
tf- Type
LogicalArray- Description
- Membership mask over A.
loc- Type
NumericArray- Description
- First-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
RunMat:ismember:LegacyOptionUnsupported- When
- Legacy compatibility options are requested.
- Message
ismember: the 'legacy' behaviour is not supported
RunMat:ismember:UnknownOption- When
- An unsupported option token is provided.
- Message
ismember: unrecognised option
RunMat:ismember:RowsColumnMismatch- When
- 'rows' mode is used and column counts differ.
- Message
ismember: inputs must have the same number of columns when using 'rows'
Behavior
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 do not match any value, including NaNs in B; their mask is false and location is zero.
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.
Types, placement and limitations
The logical mask and double first-location output have the shape of A, or one row per record in rows mode. NaNs never match, even another NaN. Complex values or rows containing any NaN component also return false and location zero. Resident outputs are restored to the first resident input owner.
Resident execution
These operations are eager fusion boundaries, not elementwise kernels or reductions, and do not broadcast. Provider availability does not imply a device-native sorting or set kernel: current providers may stage host work. An unsupported input can use exact host fallback before provider work starts. Errors after an attempt, including cancellation, are returned without repeating the operation on the host. Supported resident outputs return to their owning provider; predicates and all-indices cells have the host placement described above. Distributed execution is not supported.
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 1]; loc = [2 4; 0 1]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 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 1Return false and zero for NaN membership
[tf,loc] = ismember([NaN 1 NaN],[NaN 1]); [rows,positions] = ismember([1 NaN;2 3],[1 NaN;2 3],'rows');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?⌄
The logical mask and double first-location output have the shape of A, or one row per record in rows mode. NaNs never match, even another NaN. Complex values or rows containing any NaN component also return false and location zero. Resident outputs are restored to the first resident input owner.
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 does not implement legacy flag semantics. Passing 'legacy' or 'R2012a' raises an error, just like other set builtins in RunMat.
Will ismember run on the GPU automatically?⌄
The logical mask and double first-location output have the shape of A, or one row per record in rows mode. NaNs never match, even another NaN. Complex values or rows containing any NaN component also return false and location zero. Resident outputs are restored to the first resident input owner. These operations are eager fusion boundaries, not elementwise kernels or reductions, and do not broadcast. Provider availability does not imply a device-native sorting or set kernel: current providers may stage host work. An unsupported input can use exact host fallback before provider work starts. Errors after an attempt, including cancellation, are returned without repeating the operation on the host. Supported resident outputs return to their owning provider; predicates and all-indices cells have the host placement described above. Distributed execution is not supported.
Related Array functions
Sorting Sets
argsort · intersect · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Accumulation
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · shiftdim · squeeze · toeplitz · tril · triu · vertcat
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Combinatorics
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how ismember is executed, line by line, in Rust.
- View the source for ismember in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.