## Introduction to ismember MATLAB

In this article, we will see an outline on ismember MATLAB. Arrays are used to store the elements that belong to a specific data type. In Matlab, elements in the array are stored in the form of rows and columns. For example: if the array has 3 rows and 2 columns then it is known as 3 by 2 array. The elements are stored in their respective memory locations. There are various types of arrays in Matlab which can also store the elements of different types and dimensions, which are known as cell arrays. There are various functions associated with the array in Matlab.

### ismember Function in Matlab

In Matlab, we can check if a particular element belongs to an array or not by using ismember () function. The result is in the form of logical 1 (True) or logical 0 (False). Please find the below syntaxes which are used in Matlab considering ismember function:

**Lx=ismember (X, Y):**This checks whether the elements in X is present in Y. If the elements are present, then it returns 1(True) else it returns 0(False). If X and Y are in the form of tables or timetables, then it returns the logical value for every row present and if X and Y are in the form of timetables, then row times are considered. The resultant value Lx is a column vector.**Lx=ismember (X, Y, rows):**This syntax considers the rows of X and Y as single entities and determines the logical values which are in the form of 1 and 0. If the values are present, then it returns logical 1 (True) else it returns logical 0 (False). The rows option is not valid if cell arrays are used, provided that the input array is categorical or datetime array.**[Lx, LocationY] =ismember (____):**Here LocationY is used to find the lowest index values present in Y if the values present in X are a member of Y. If the value is 0, then it indicates that the elements present in X are not a part of Y. If there are rows option specified in the syntax, then LocationY contains the lowest index of Y, provided that rows in X should be a part of rows in Y. If the value is 0, then X is not in the row of Y. If X and Y are in the form of tables or timetables, then LocationY contains the lowest index of Y, provided that rows in X should be a part of rows in Y. If the value is 0, then X is not in the row of Y.**[Lx, LocationY] =ismember (____,” legacy option”):**If the legacy option is mentioned in the syntax, then the properties and behaviour of ismember function is considered from R2012b versions and previous releases. Legacy option is not supported if the types of arrays are categorical, datetime, duration, timetables or tables.

### Examples of ismember Function in MATLAB

Below examples explain the concept of ismember function in Matlab:

#### Example #1

To check whether the elements of X are present in Y.

**Code:**

`X = [4 6 3 2];`

Y = [1 2 4 5 3 8];

LX = ismember(X,Y)

**Output:**

In the above example, ismember function checks whether the elements present in X are also present in Y and returns the logical values in the form of 1 and 0. The first value of X i.e. 4 is present in Y, so the first value of the resultant LX is 1. Similarly, the values at the 3^{rd} and 4^{th} positions are also present in the Y, so the resultant values are 1. Since the value at 2^{nd} position i.e. 6 is not present in Y, so the resultant value is 0.

4.5 (4,418 ratings)

View Course

#### Example #2

To determine the corresponding location of the values that are present in Y array.

**Code:**

`X = [4 6 3 2];`

Y = [1 2 4 5 3 8];

[LX,LocY] = ismember(X,Y)

**Output:**

In the above example, ismember function first checks whether the values present in X are also a part of Y or not. After checking that, it returns the resultant values in the form of 1 and 0 which can be seen for LX. After that, we have given another variable in syntax to determine the lowest index of the values of X that are present in Y. So, the first element of X i.e.4 is present in Y at only position 3, so it will return 3. The second element which is present in Y is 3 and the respective position is 5, 2 is present in the 2^{nd} position in Y. If the values are not present in Y, then the location value will be 0.

The input arrays can be logical, numeric, character, string, datetime, categorical, tables, timetables, duration, etc. If the rows option is specified in the syntax, then the input arrays should have the same number of columns. The class of the input arrays should be the same with some exceptions:

- If one of the arrays is of type double then they can be combined with char, logical and numeric classes.
- Character or string arrays can be combined with cell arrays if they are of character vectors.
- If the array is of type categorical, then they can be included with string, character or cell arrays.
- If the arrays are of type categorical, then the type of categories and their order of values should be the same.
- If Y is of type table or timetable, then the variable names should be the same as that of X. If the input array is of type datetime then they should be consistent.

There are other exceptions as well, which should be taken into account before dealing with ismember function in Matlab.

#### Example #3

To check whether the rows using ismember function.

**Code:**

`X = [1 2 4 7; 3 4 5 9];`

Y = [13 4 5 9; 0 3 8 7; 3 4 5 9];

[LX, LocY] = ismember(X,Y, 'rows')

**Output:**

### Conclusion

Ismember function is an important part of the Arrays topic present in Matlab. It is better to know about all the exceptions and rules before working with the above function since there are many data type exceptions and the functions associated with it.

### Recommended Articles

This is a guide to ismember MATLAB. Here we discuss the Introduction and ismember functions in MATLAB along with its examples respectively. You can also go through our suggested articles to learn more –