Updated March 24, 2023

## Introduction to Find Function Matlab

MATLAB provides its user with a basket of functions, in this article we will understand a powerful function called ‘Find’. In its simplest form, find function will return the indices of array X that points to the nonzero elements. If it finds none, the function will return an empty matrix.

**Syntax of Find Function:**

`R = find(X)`

`R = find (X, n)`

`R = find (X, n,direction)`

`[row, col] = find ()`

`[row, col, v] = find ()`

### Description of Find in Matlab

Below will learn all the Find function in Matlab one by one accordingly:

#### 1. R = find(A)

- Here A is an array, this function will return a vector that will contain linear indices of each non zero elements of A.
- Let’s assume A to be a vector then R will return a vector which will have the same orientation as x.
- If A is to be a multidimensional array, R will give back a column vector containing linear indices.
- If A has all zeros or empty values, then R will give us an empty array.

Let us now understand this concept with an example:

Here, X is a 3 x 3 matrix:

**Code:**

```
X = [2 1 1; 0 3 1; 0 0 1]
k = find(X)
```

Below is how the console will look like:

**Output:**

Let us use a logical operator ~ to locate the zero values.

**Code:**

```
X = [2 1 1; 0 3 1; 0 0 1]
R1 = find(~X)\
```

**Output:**

#### 2. R = find (X, n)

This function will return the first n indices for the non zero values in X. Below is an example to understand this find function:

X is a 3 x 3 matrix:

**Code:**

```
X=[8 1 6;3 5 7;4 9 2]
k = find(X < 10, 5)
```

**Output:**

#### 3. R = find (X, n,direction)

This function will return the n indices for non zero elements in either last or first direction as specified in the function. Below is an example to understand this find function:

**Code:**

X is a 3 x 3 matrix:

```
X=[0 1 0;3 0 7;0 9 0]
k = find (X, 3, last)
```

**Output:**

#### 4. [row, col] = find()

This function will return the row and column subscripts of non zero elements of array X for any of the above-mentioned functions. Below is an example to understand this find function:

**Code:**

X is a 3 x 3 matrix:

```
X=[1 3 1;8 0 1;5 4 6]
[row, col] = find(X>0 & X<5, 3)
```

**Output:**

#### 5 . [row, col, v] = find()

This function will return vector ‘v’ in addition to the row and column subscripts of non zero elements of array X for any of the above-mentioned functions. Below is an example to understand this find function:

**Code:**

X is a 3 x 3 matrix:

```
X=[1 3 0;8 0 1;0 4 0]
[row, col, v] = find(X)
```

**Output:**

### Conclusion

We can find elements of arrays that satisfy a defined condition. For achieving this, we need to use find function along with a relational expression. So, find(A<4) will return linear indices of the elements of A which are less than 4. We can also directly find elements in A that fulfill the given condition like A<5. For this, we simply use A(A<5). Here we can avoid making calls to function like A(find(A<5)) When we execute find function with any relational operation like A>1, we must remember that here, the result will be a matrix of 1s & 0s. Example: [row, col, A] = find(A>1) will return column vector with logical true values.

### Recommended Articles

This is a guide to Find Function Matlab. Here we discuss the introduction and description of find in Matlab with proper codes and outputs. You can also go through our other related articles to learn more –