find
Find indices and values of nonzero elements
Description
returns a vector containing the linear indices of each nonzero element in array
k = find(X)X.
If
Xis a vector, thenfindreturns a vector with the same orientation asX.If
Xis a multidimensional array, thenfindreturns a column vector of the linear indices of the result.
Examples
Input Arguments
Output Arguments
More About
Tips
To find array elements that meet a condition, use
findin conjunction with a relational expression. For example,find(X<5)returns the linear indices to the elements inXthat are less than5.To directly find the elements in
Xthat satisfy the conditionX<5, useX(X<5). Avoid function calls likeX(find(X<5)), which unnecessarily usefindon a logical matrix.When you execute
findwith a relational operation likeX>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 logical1(true) values forv.The row and column subscripts,
rowandcol, are related to the linear indices inkbyk = sub2ind(size(X),row,col).
Extended Capabilities
Version History
Introduced before R2006a