ismember

Syntax

Description

Lia = ismember(A,B) returns
an array containing logical 1 (true)
where the data in A is found in B.
Elsewhere, the array contains logical 0 (false).

If A and B are
tables or timetables, then ismember returns a logical
value for each row. For timetables, ismember takes
row times into account to determine equality. The output, Lia,
is a column vector.

Lia = ismember(A,B,'rows') treats
each row of A and each row of B as
single entities and returns a column vector containing logical 1 (true)
where the rows of A are also rows of B.
Elsewhere, the array contains logical 0 (false).

The 'rows' option does not support cell arrays,
unless one of the inputs is either a categorical array or a datetime
array.

Query array, specified as a numeric array, logical array, character
array, string array, categorical array, datetime array, duration array,
cell array of character vectors, table, or timetable. If you specify
the 'rows' option, A and B must
have the same number of columns.

A must belong to the same class as B with
the following exceptions:

logical, char,
and all numeric classes can combine with double arrays.

Cell arrays of character vectors can combine with
character arrays or string arrays.

Categorical arrays can combine with character arrays,
cell arrays of character vectors, or string arrays.

Datetime arrays can combine with cell arrays of date
character vectors or single date character vectors.

There are additional requirements for A and B based
on data type:

If A and B are
both ordinal categorical arrays, they must have the same sets of categories,
including their order. If neither A nor B are
ordinal, they need not have the same sets of categories, and the comparison
is performed using the category names.

If A is a table or timetable,
it must have the same variable names as B (except
for order). For tables, row names are ignored, so that two rows that
have the same values, but different names, are considered equal. For
timetables, row times are taken into account, so that two rows that
have the same values, but different times, are not considered equal.

If A and B are
datetime arrays, they must be consistent with each other in whether
they specify a time zone.

For textual inputs, ismember generally does not ignore trailing spaces in character vectors, cell arrays of character vectors, and string arrays. However, there are a few cases when ismember does ignore trailing spaces:

If A is a character array and B is a cell array of character vectors, then ismember ignores trailing spaces in the character array.

When the 'rows' option is specified, ismember ignores trailing spaces in character vectors and character arrays.

A also can be an object with the following
class methods:

sort (or sortrows for
the 'rows' option)

eq

ne

The object class methods must be consistent with each
other. These objects include heterogeneous arrays derived from the
same root class. For example, A can be an array
of handles to graphics objects.

Set array, specified as a numeric array, logical array, character
array, string array, categorical array, datetime array, duration array,
cell array of character vectors, table, or timetable. If you specify
the 'rows' option, A and B must
have the same number of columns.

B must belong to the same class as A with
the following exceptions:

logical, char,
and all numeric classes can combine with double arrays.

Cell arrays of character vectors can combine with
character arrays or string arrays.

Categorical arrays can combine with character arrays,
cell arrays of character vectors, or string arrays.

Datetime arrays can combine with cell arrays of date
character vectors or single date character vectors.

There are additional requirements for A and B based
on data type:

If A and B are
both ordinal categorical arrays, they must have the same sets of categories,
including their order. If neither A nor B are
ordinal, they need not have the same sets of categories, and the comparison
is performed using the category names.

If B is a table or timetable,
it must have the same variable names as A (except
for order). For tables, row names are ignored, so that two rows that
have the same values, but different names, are considered equal. For
timetables, row times are taken into account, so that two rows that
have the same values, but different times, are not considered equal.

If A and B are
datetime arrays, they must be consistent with each other in whether
they specify a time zone.

For textual inputs, ismember generally does not ignore trailing spaces in character vectors, cell arrays of character vectors, and string arrays. However, there are a few cases when ismember does ignore trailing spaces:

If A is a character array and B is a cell array of character vectors, then ismember ignores trailing spaces in the character array.

When the 'rows' option is specified, ismember ignores trailing spaces in character vectors and character arrays.

B also can be an object with the following
class methods:

sort (or sortrows for
the 'rows' option)

eq

ne

The object class methods must be consistent with each
other. These objects include heterogeneous arrays derived from the
same root class. For example, B can be an array
of handles to graphics objects.

Output Arguments

Lia — Logical index to Avector | matrix | N-D array

Logical index to A, returned as a vector,
matrix or N-D array containing logical 1 (true)
wherever the values (or rows) in A are members
of B. Elsewhere, it contains logical 0 (false).

Lia is an array of the same size as A,
unless you specify the 'rows' flag.

If the 'rows' flag is specified or if A is
a table or timetable, Lia is a column vector with
the same number of rows as A.

Locb — Locations in Bvector | matrix | N-D array

Locations in B, returned as a vector, matrix,
or N-D array. If the 'legacy' flag is not specified, Locb contains
the lowest indices to the values (or rows) in B that
are found in A. Values of 0 indicate
where A is not a member of B.

Locb is an array of the same size as A unless
you specify the 'rows' flag.

If the 'rows' flag is specified or if A is
a table or timetable, Locb is a column vector with
the same number of rows as A.

Tips

Use ismembertol to perform comparisons between
floating-point numbers using a tolerance.

To find the rows from table or timetable A that
are found in B with respect to a subset of variables,
you can use column subscripting. For example, you can use ismember(A(:,vars),B(:,vars)),
where vars is a positive integer, a vector
of positive integers, a variable name, a cell array of variable names,
or a logical vector. Alternatively, you can use vartype to create a subscript that selects
variables of a specified type.

Extended Capabilities

Tall ArraysCalculate with arrays that have more rows than fit in memory.