Categories

# 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:

1. `R = find(X)`
2. `R = find (X, n)`
3. `R = find (X, n,direction)`
4. `[row, col] = find ()`
5. `[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)\``

## 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)``

## 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)``````

## 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)``````

## 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)``````