MATLAB Function Reference |

Find indices and values of nonzero elements

**Syntax**

indices = find(X) indices = find(X, k) indices = find(X, k, 'first') indices = find(X, k, 'last') [i

`,`

j] = find(...) [i`,`

j`,`

v] = find(...)

**Description**

```
indices = find(X)
```

returns the linear indices corresponding to the nonzero entries of the array `X`

. If none are found, `find`

returns an empty, 0-by-1 matrix. In general, `find(X)`

regards `X`

as `X(:)`

, which is the long column vector formed by concatenating the columns of `X`

.

`indices = find(X, k)`

or `indices = find(X, k, 'first')`

returns at most the first `k`

indices corresponding to the nonzero entries of `X`

. `k`

must be a positive integer, but it can be of any numeric data type.

`indices = find(X, k, 'last')`

returns at most the last `k`

indices corresponding to the nonzero entries of `X`

.

```
[i,j] = find(...)
```

returns the row and column indices of the nonzero entries in the matrix `X`

. This syntax is especially useful when working with sparse matrices. If `X`

is an N-dimensional array with N > 2, `j`

contains linear indices for the dimensions of `X`

other than the first.

```
[i,j,v] = find(...)
```

returns a column vector `v`

of the nonzero entries in `X`

, as well as row and column indices.

**Examples**

returns linear indices for the nonzero entries of `X`

.

You can use a logical expression to define `X`

. For example,

returns linear indices corresponding to the entries of `X`

that are greater than 2.

a vector of row indices of the nonzero entries of `X`

,

a vector of column indices of the nonzero entries of `X`

, and

a vector containing the nonzero entries of `X`

.

returns the indices of the first four entries of `M`

that are greater than 3.

If `X`

is a vector of all zeros, `find(X)`

returns an empty, 0-by-1 matrix. For example,

**See Also**

`nonzeros`

, `sparse`

, `colon`

, logical operators (elementwise and short-circuit), relational operators, `ind2sub`

filter2 | findall |

© 1994-2005 The MathWorks, Inc.