Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Find indices and values of nonzero elements

`k = find(X)`

`k = find(X,n)`

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

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

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

returns
a vector containing the linear
indices of each nonzero element in array `k`

= find(`X`

)`X`

.

If

`X`

is a vector, then`find`

returns a vector with the same orientation as`X`

.If

`X`

is a multidimensional array, then`find`

returns a column vector of the linear indices of the result.If

`X`

contains no nonzero elements or is empty, then`find`

returns an empty array.

To find array elements that meet a condition, use

`find`

in conjunction with a relational expression. For example,`find(X<5)`

returns the linear indices to the elements in`X`

that are less than`5`

.To directly find the elements in

`X`

that satisfy the condition`X<5`

, use`X(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 is 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`k`

by`k = sub2ind(size(X),row,col)`

.

Was this topic helpful?