ismember - Find set members of data - MATLAB (original) (raw)
Syntax
Description
[Lia](#btcnv43-1-Lia) = ismember([A](#btcnv43-1-A),[B](#btcnv43-1-B))
returns an array containing logical 1
(true
) where the data in A
is found inB
. Elsewhere, the array contains logical0
(false
).
- If
A
andB
are tables or timetables, thenismember
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](#btcnv43-1-Lia) = ismember([A](#btcnv43-1-A),[B](#btcnv43-1-B),"rows")
treats each row of A
and each row of B
as single entities and returns a column vector containing logical1
(true
) where the rows ofA
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.
[[Lia](#btcnv43-1-Lia),[Locb](#btcnv43-1-Locb)] = ismember(___)
also returns an array, Locb
, using any of the previous syntaxes.
- Generally,
Locb
contains the lowest index inB
for each value inA
that is a member ofB
. Values of0
indicate whereA
is not a member ofB
. - If the
"rows"
option is specified, thenLocb
contains the lowest index inB
for each row inA
that is also a row inB
. Values of0
indicate whereA
is not a row ofB
. - If
A
andB
are tables or timetables, thenLocb
contains the lowest index inB
for each row inA
that is also a row inB
. Values of0
indicate whereA
is not a row ofB
.
[[Lia](#btcnv43-1-Lia),[Locb](#btcnv43-1-Locb)] = ismember(___,"legacy")
preserves the behavior of theismember
function from R2012b and prior releases using any of the input arguments in previous syntaxes.
The 'legacy'
option does not support categorical arrays, datetime arrays, duration arrays, tables, or timetables.
Examples
Create two vectors with values in common.
A = [5 3 4 2]; B = [2 4 4 4 6 8];
Determine which elements of A
are also in B
.
Lia = 1×4 logical array
0 0 1 1
A(3)
and A(4)
are found in B
.
Create two tables with rows in common.
A = table([1:5]',["red";"green";"blue";"cyan";"magenta"],logical([0;1;0;1;0]))
A=5×3 table Var1 Var2 Var3 ____ _________ _____
1 "red" false
2 "green" true
3 "blue" false
4 "cyan" true
5 "magenta" false
B = table([1:2:10]',["red";"blue";"magenta";"yellow";"black"],logical(zeros(5,1)))
B=5×3 table Var1 Var2 Var3 ____ _________ _____
1 "red" false
3 "blue" false
5 "magenta" false
7 "yellow" false
9 "black" false
Determine which rows of A
are also in B
.
Lia = 5×1 logical array
1 0 1 0 1
The rows A(1,:)
, A(3,:)
, and A(5,:)
are found in B
.
Create two vectors with values in common.
A = [5 3 4 2]; B = [2 4 4 4 6 8];
Determine which elements of A
are also in B
as well as their corresponding locations in B
.
[Lia,Locb] = ismember(A,B)
Lia = 1×4 logical array
0 0 1 1
The lowest index to A(3)
is B(2)
, and A(4)
is found in B(1)
.
Create a vector x
. Obtain a second vector y
by transforming and untransforming x
. This transformation introduces round-off differences in y
.
x = (1:6)'*pi; y = 10.^log10(x);
Verify that x
and y
are not identical by taking the difference.
ans = 6×1 10-14 ×
0.0444
0
0
0
0
-0.3553
Use ismember
to find the elements of x
that are in y
. The ismember
function performs exact comparisons and determines that some of the matrix elements in x
are not members of y
.
lia = 6×1 logical array
0 1 1 1 1 0
Use ismembertol
to perform the comparison using a small tolerance. ismembertol
treats elements that are within tolerance as equal and determines that all of the elements in x
are members of y
.
LIA = 6×1 logical array
1 1 1 1 1 1
Create a table, A
, of smoker status, age, and height for five people.
A = table([true;true;false;true;false],[27;52;31;46;35],[74;68;64;61;64],... VariableNames=["Smoker" "Age" "Height"],... RowNames=["Sanchez" "Johnson" "Li" "Diaz" "Brown"])
A=5×3 table Smoker Age Height ______ ___ ______
Sanchez true 27 74
Johnson true 52 68
Li false 31 64
Diaz true 46 61
Brown false 35 64
Create another table, B
, with rows in common with A
.
B = table([false;false;false;false],[47;31;35;23],[68;64;62;58],... VariableNames=["Smoker" "Age" "Height"],... RowNames=["Nguyen" "Cohen" "Jones" "Garcia"])
B=4×3 table Smoker Age Height ______ ___ ______
Nguyen false 47 68
Cohen false 31 64
Jones false 35 62
Garcia false 23 58
Determine which rows of A
are also in B
, as well as their corresponding locations in B
.
[Lia,Locb] = ismember(A,B)
Lia = 5×1 logical array
0 0 1 0 0
Two table rows that have the same values, but different row names, are considered equal. The same data for Li
is found in B(2,:)
, which corresponds to Cohen
.
Create two matrices with a row in common.
A = [1 3 5 6; 2 4 6 8]; B = [2 4 6 8; 1 3 5 7; 2 4 6 8];
Determine which rows of A
are also in B
as well as their corresponding locations in B
.
[Lia, Locb] = ismember(A,B,"rows")
Lia = 2×1 logical array
0 1
The lowest index to A(2,:)
is B(1,:)
.
Create two vectors containing NaN
.
A = [5 NaN NaN]; B = [5 NaN NaN];
Determine which elements of A
are also in B
, as well as their corresponding locations in B
.
[Lia,Locb] = ismember(A,B)
Lia = 1×3 logical array
1 0 0
ismember
treats NaN
values as distinct.
Create a string array, A
.
A = ["dog","cat","fish","horse"];
Create another string array, B
, where some elements have trailing white space.
B = ["dog ","cat","fish ","horse"];
Determine which strings in A
are also in B
.
[Lia,Locb] = ismember(A,B)
Lia = 1×4 logical array
0 1 0 1
ismember
treats trailing white space in string arrays, and also in cell arrays of character vectors, as distinct characters.
Use the "legacy"
flag to preserve the behavior of ismember
from R2012b and prior releases in your code.
Find the members of B
with the current behavior.
A = [5 3 4 2]; B = [2 4 4 4 6 8]; [Lia1,Locb1] = ismember(A,B)
Lia1 = 1×4 logical array
0 0 1 1
Find the members of B
, and preserve the legacy behavior.
[Lia2,Locb2] = ismember(A,B,"legacy")
Lia2 = 1×4 logical array
0 0 1 1
Input Arguments
Query array, specified as an array, table, or timetable. If you specify the "rows"
option, A
andB
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 withdouble
arrays.- String arrays can combine with character vectors and cell arrays of character vectors.
- Categorical arrays can combine with string arrays, character vectors, and cell arrays of character vectors.
- Datetime and duration arrays can combine with string scalars and character vectors that are formatted to represent dates and times.
There are additional requirements for A
andB
based on data type:
- If
A
is a categorical array, thenB
can be a categorical array, a string array, a character vector, or a cell array of character vectors. In this case, the second inputB
is a set of category names. - If
A
andB
are both ordinal categorical arrays, they must have the same sets of categories, including their order.
If neitherA
norB
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 asB
(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
andB
are datetime arrays, then both arrays must either specify time zones or be unzoned.
A
also can be an object with the following class methods:
sort
(orsortrows
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 an array, table, or timetable. If you specify the"rows"
option, A
andB
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 withdouble
arrays.- String arrays can combine with character vectors and cell arrays of character vectors.
- Categorical arrays can combine with string arrays, character vectors, and cell arrays of character vectors.
- Datetime and duration arrays can combine with string scalars and character vectors that are formatted to represent dates and times.
There are additional requirements for A
andB
based on data type:
- If
A
is a categorical array, thenB
can be a categorical array, a string array, a character vector, or a cell array of character vectors. In this case, the second inputB
is a set of category names. - If
A
andB
are both ordinal categorical arrays, they must have the same sets of categories, including their order.
If neitherA
norB
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 asB
(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
andB
are datetime arrays, then both arrays must either specify time zones or be unzoned.
B
also can be an object with the following class methods:
sort
(orsortrows
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
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 ofB
. Elsewhere, it contains logical0
(false
).
Lia
is an array of the same size asA
, unless you specify the "rows"
flag.
If the "rows"
flag is specified or ifA
is a table or timetable, Lia
is a column vector with the same number of rows as A
.
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 asA
unless you specify the "rows"
flag.
If the "rows"
flag is specified or ifA
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 inB
with respect to a subset of variables, you can use column subscripting. For example, you can useismember(A(:,_`vars`_),B(:,_`vars`_))
, wherevars
is a positive integer, a vector of positive integers, a variable name, a string array or 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
Theismember
function supports tall arrays with the following usage notes and limitations:
- If both
A
andB
are tall arrays, then one must be the result of a reduction operation applied in the first dimension (such assum
,prod
,max
, and so on).
For more information, see Tall Arrays.
The ismember
function supports GPU array input with these usage notes and limitations:
- The
"legacy"
flag is not supported. - 64-bit integers are not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a