| MATLAB Function Reference | Search  Help Desk |
| find | Examples See Also |
Find indices and values of nonzero elements
k = find(x) [i,j] = find(X) [i,j,v] = find(X)
k = find(X)
returns the indices of the array x that point to nonzero elements. If none is found, find returns an empty matrix.
[i,j] = find(X)
returns the row and column indices of the nonzero entries in the matrix X. This is often used with sparse matrices.
[i,j,v] = find(X)
returns a column vector v of the nonzero entries in X, as well as row and column indices.
In general, find(X) regards X as X(:), which is the long column vector formed by concatenating the columns of X.
[i,j,v] = find(X~=0) produces a vector v with all 1s, and returns the row and column indices.
Some operations on a vector
x = [11 0 33 0 55]';
find(x)
ans =
1
3
5
find(x == 0)
ans =
2
4
find(0 < x & x < 10*pi)
And on a matrix
M = magic(3)
M =
8 1 6
3 5 7
4 9 2
[i,j,m] = find(M > 6)
i = j = m =
1 1 1
3 2 1
2 3 1
The relational operators <, <=,>,>=,==, ~=, and:
nonzeros Nonzero matrix elements
sparse Create sparse matrix