Accelerating the pace of engineering and science

# Documentation Center

• Trial Software
• Product Updates

# ismember

Array elements that are members of set array

## Syntax

• Lia = ismember(A,B) example
• Lia = ismember(A,B,'rows')
• [Lia,Locb] = ismember(A,B) example
• [Lia,Locb] = ismember(A,B,'rows') example
• [Lia,Locb] = ismember(___,'legacy') example

## Description

example

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

• If A and B are numeric arrays, logical arrays, character arrays, categorical arrays, or cell arrays of strings, then ismember returns a logical value for each element of A. The output, Lia, is an array of the same size as A.

• If A and B are tables, then ismember returns a logical value for each row of A. The output, Lia, is a column vector with the same number of rows as A.

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

A and B must have the same number of columns when you use the 'rows' option. Furthermore, the 'rows' option does not support cell arrays.

example

[Lia,Locb] = ismember(A,B) also returns an array, Locb.

• If A and B are numeric arrays, logical arrays, character arrays, categorical arrays, or cell arrays of strings, then Locb contains the lowest index in B for each value in A that is a member of B. The output array, Locb, contains 0 wherever A is not a member of B.

• If A and B are tables, then Locb contains the lowest index in B for each row in A that is also a row in B. The output vector, Locb, contains 0 whenever A is not a row of B.

example

[Lia,Locb] = ismember(A,B,'rows') also returns a column vector, Locb, containing the lowest index in B for each row in A that is also a row in B. The output vector, Locb, contains 0 wherever A is not a row of B.

example

[Lia,Locb] = ismember(___,'legacy') preserves the behavior of the ismember function from R2012b and prior releases using any of the input arguments in previous syntaxes.

The 'legacy' option does not support categorical arrays or tables.

## Examples

expand all

### Values That Are Members of Set

Define 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 = ismember(A,B)
```
```Lia =

0     0     1     1```

A(3) and A(4) are found in B.

### Table Rows Found in Another Table

Define two tables with rows in common.

```A = table([1:5]',['A';'B';'C';'D';'E'],logical([0;1;0;1;0]))
B = table([1:2:10]',['A';'C';'E';'G';'I'],logical(zeros(5,1)))```
```A =

Var1    Var2    Var3
----    ----    -----
1       A       false
2       B       true
3       C       false
4       D       true
5       E       false

B =

Var1    Var2    Var3
----    ----    -----
1       A       false
3       C       false
5       E       false
7       G       false
9       I       false```

Determine which rows of A are also in B.

```Lia = ismember(A,B)
```
```Lia =

1
0
1
0
1```

A(1,:), A(3,:), and A(5,:) are found in B.

### Members of Set and Indices to Values

Define 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 =

0     0     1     1

Locb =

0     0     2     1```

The lowest index to A(3) is B(2).

A(4) is found in B(1).

### Rows of Another Table and Their Location

Define a table, A, of gender, age, and height for five people.

```A = table(['M';'M';'F';'M';'F'],[27;52;31;46;35],[74;68;64;61;64],...
'VariableNames',{'Gender' 'Age' 'Height'},...
'RowNames',{'Ted' 'Fred' 'Betty' 'Bob' 'Judy'})```
```A =

Gender    Age    Height
------    ---    ------
Ted      M         27     74
Fred     M         52     68
Betty    F         31     64
Bob      M         46     61
Judy     F         35     64    ```

Define a table, B, with rows in common with A.

```B = table(['M';'F';'F';'F'],[47;31;35;23],[68;64;62;58],...
'VariableNames',{'Gender' 'Age' 'Height'},...
'RowNames',{'Joe' 'Meg' 'Beth' 'Amy'})```
```B =

Gender    Age    Height
------    ---    ------
Meg     F         31     64
Joe     M         47     68
Beth    F         35     62
Amy     F         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 =

0
0
1
0
0

Locb =

0
0
2
0
0```

Two rows that have the same values, but different names, are considered equal. The same data for Betty is found in B(2,:), which corresponds to Meg.

### Rows That Belong to a Set

Define 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 =

0
1

Locb =

0
1
```

The lowest index to A(2,:) is B(1,:).

### Members of Set Containing NaN Values

Define 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     0     0

Locb =

1     0     0```

ismember treats NaN values as distinct.

### Cell Array of Strings with Trailing White Space

Create a cell array of strings, A.

```A = {'dog','cat','fish','horse'};
```

Create a cell array of strings, B, where some of the strings have trailing white space.

`B = {'dog ','cat','fish ','horse'};`

Determine which strings of A can be found in B.

`[Lia,Locb] = ismember(A,B)`
```Lia =

0     1     0     1

Locb =

0     2     0     4```

ismember treats trailing white space in cell arrays of strings as distinct characters.

### Members of Char and Cell Array of Strings

Create a character array, A.

```A = ['cat';'dog';'fox';'pig'];
```

Create a cell array of strings, B.

```B = {'dog','cat','fish','horse'};
```

Determine which strings of A can be found in B.

`[Lia,Locb] = ismember(A,B)`
```Lia =

1
1
0
0

Locb =

2
1
0
0```

### Preserve Legacy Behavior of ismember

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 =

0     0     1     1

Locb1 =

0     0     2     1```

Find the members of B, and preserve the legacy behavior.

```[Lia2,Locb2] = ismember(A,B,'legacy')
```
```Lia2 =

0     0     1     1

Locb2 =

0     0     4     1```

## Input Arguments

expand all

### A — Query arraynumeric array | logical array | character array | categorical array | cell array of strings | table

Query array, specified as a numeric array, logical array, character array, categorical array, cell array of strings, or a table.

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 strings can combine with char arrays.

• Categorical arrays can combine with cell arrays of strings or single strings.

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 you specify the 'rows' option, A must have the same number of columns as B.

If A is a table, it must have the same variable names as B. Conversely, the row names do not matter. Two rows that have the same values, but different names, are considered equal.

Furthermore, A 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.

### B — Set arraynumeric array | logical array | character array | categorical array | cell array of strings | table

Set array, specified as a numeric array, logical array, character array, categorical array, cell array of strings, or a table.

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 strings can combine with char arrays.

• Categorical arrays can combine with cell arrays of strings or single strings.

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 you specify the 'rows' option, B must have the same number of columns as A.

If B is a table, it must have the same variable names as A. Conversely, the row names do not matter. Two rows that have the same values, but different names, are considered equal.

Furthermore, B 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.

## Output Arguments

expand all

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

Logical index to A, returned as a vector, matrix or N-D array containing 1 (true) wherever the values (or rows) in A are members of B. Elsewhere, it returns 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, 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. Locb contains 0 wherever 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, Locb is a column vector with the same number of rows as A.

## More About

expand all

### Tips

• To find the rows from table 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.

## See Also

Was this topic helpful?