Accelerating the pace of engineering and science

# find

Find indices and values of nonzero elements

## Description

example

I = find(X) returns a vector containing the linear indices of each nonzero element in array X.

example

I = find(X,k) returns the first k indices corresponding to the nonzero elements in X.

example

I = find(X,k,direction) specifies the search direction as either 'first' or 'last'. For example, find(X,k,'last') finds the last k nonzero elements in X.

example

[row,col] = find(___) returns the row and column subscripts of each nonzero element in array X using any of the previous syntaxes.

example

[row,col,v] = find(___) also returns vector v that contains the nonzero elements of X.

## Examples

expand all

### Zero and Nonzero Elements in Matrix

Find the nonzero elements in a 3-by-3 matrix.

`X = [1 0 2; 0 1 1; 0 0 4]`
```X =

1     0     2
0     1     1
0     0     4```
`I = find(X)`
```I =

1
5
7
8
9```

Use the logical not operator on X to locate the zeros.

`I2 = find(~X)`
```I2 =

2
3
4
6```

### Find Elements Satisfying a Condition

Find the first 5 elements in a 4-by-4 magic square matrix that are less than 10.

```X = magic(4)
```
```X =

16     2     3    13
5    11    10     8
9     7     6    12
4    14    15     1```
`I = find(X<10,5)`
```I =

2
3
4
5
7```

View the values of the elements referenced by I.

`X(I)`
```ans =

5
9
4
2
7```

### Elements Equal to Specific Values

To find a specific integer value, you can use the == operator. For instance, find the element equal to 13 in a 1-by-10 vector of odd integers.

```X = 1:2:20
```
```X =

1     3     5     7     9    11    13    15    17    19```
`I = find(X==13)`
```I =

7```

To find a noninteger value, use a tolerance value (such as eps). Otherwise, the result is sometimes an empty matrix due to floating-point roundoff error.

```Y = 0:0.1:1
```
```Y =

0    0.1000    0.2000    0.3000    0.4000    0.5000    0.6000    0.7000    0.8000    0.9000    1.0000```
`I = find(Y==0.3)`
```I =

Empty matrix: 1-by-0```
`I = find(abs(Y-0.3) < eps)`
```I =

4```

### Find Last Several Nonzero Elements

Create a 6-by-6 magic square matrix with all of the odd-indexed elements equal to zero.

```X = magic(6);
X(1:2:end) = 0```
```X =

0     0     0     0     0     0
3    32     7    21    23    25
0     0     0     0     0     0
8    28    33    17    10    15
0     0     0     0     0     0
4    36    29    13    18    11```

Locate the last 4 nonzeros.

`I = find(X,4,'last')`
```I =

30
32
34
36```

### Find Elements Satisfying Multiple Conditions

Find the first 3 elements in a 4-by-4 matrix that are greater than 0 and less than 10. Specify two outputs to return the row and column subscripts to the elements.

```X = [18 3 1 11; 8 10 11 3; 9 14 6 1; 4 3 15 21]
```
```X =

18     3     1    11
8    10    11     3
9    14     6     1
4     3    15    21
```
`[row,col] = find(X>0 & X<10,3)`
```row =

2
3
4

col =

1
1
1```

The first instance is X(2,1), which is 8.

### Subscripts and Values for Nonzero Elements

Find the nonzero elements in a 3-by-3 matrix. Specify three outputs to return the row subscripts, column subscripts, and element values.

```X = [3 2 0; -5 0 7; 0 0 1]
```
```X =

3     2     0
-5     0     7
0     0     1```
`[row,col,v] = find(X)`
```row =

1
2
1
2
3

col =

1
1
2
3
3

v =

3
-5
2
7
1```

## Input Arguments

expand all

### X — Input arrayscalar | vector | matrix | multidimensional array

Input array, specified as a scalar, vector, matrix, or multidimensional array. If X is an empty array or has no nonzero elements, then I is an empty array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char
Complex Number Support: Yes

### k — Number of nonzeros to findpositive integer scalar

Number of nonzeros to find, specified as a positive integer scalar. By default, find(X,k) looks for the first k nonzero elements in X.

### direction — Search direction'first' (default) | 'last'

Search direction, specified as the string 'first' or 'last'. Look for the last k nonzero elements in X using find(X,k,'last').

Data Types: char

## Output Arguments

expand all

### I — Linear indices to nonzero elementsvector

Linear indices to nonzero elements, returned as a vector. If X is a row vector, then I is also a row vector. Otherwise, I is a column vector. I is an empty array when X is an empty array or has no nonzero elements.

You can return the nonzero values in X using X(I).

### row — Row subscriptsvector

Row subscripts, returned as a vector. Together, row and col specify the X(row,col) subscripts corresponding to the nonzero elements in X.

### col — Column subscriptsvector

Column subscripts, returned as a vector. Together, row and col specify the X(row,col) subscripts corresponding to the nonzero elements in X.

### v — Nonzero elements of Xvector

Nonzero elements of X, returned as a vector.

## More About

expand all

### Linear Indices

A linear index allows use of a single subscript to index into an array, such as A(k). MATLAB treats the array as a single column vector with each column appended to the bottom of the previous column. Thus, linear indexing numbers the elements in the columns from top to bottom, left to right.

For example, consider a 3-by-3 matrix. You can reference the A(2,2) element with A(5), and the A(2,3) element with A(8). The linear index changes depending on the size of the array; A(5) returns a differently located element for a 3-by-3 matrix than it does for a 4-by-4 matrix.

The sub2ind and ind2sub functions are useful in converting between subscripts and linear indices.

### Tips

• Use find in conjunction with a relational expression to find array elements that meet a condition. For example, find(X<5) returns the linear indices to the elements in X that are less than 5.

• Use X(X<5) to directly find the elements in X that satisfy the condition X<5. Avoid function calls like X(find(X<5)), which unnecessarily use find on a logical matrix.

• When you execute find with a relational operation like X>1, it's important to remember that the result of the relational operation is a logical matrix of ones and zeros. For example, the command [row,col,v] = find(X>1) returns a column vector of logical 1 (true) values for v.

• The row and column subscripts, row and col, are related to the linear indices in I by I = sub2ind(size(X),row,col).

## See Also

Was this topic helpful?