Find indices and values of nonzero elements

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.

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

.