find

Purpose

Find indices and values of nonzero elements.

Synopsis

k = find(X)
[i,j] = find(X)
[i,j,s] = find(X)

Description

k = find(X) returns the indices of the vector X that point to nonzero elements. If none is found, find returns an empty matrix. If X is a matrix, find regards X as X(:), which is the long column vector formed by concatenating the columns of X.

[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,s] = find(X) also returns a column vector of the nonzero entries in X. Note that find(X) and find(X ~= 0) produce the same i and j, but the latter produces an s with all 1s.

Examples

Some operations on a vector

x = [11  0  33  0  55]';
          
find(x) =
    1
    3
    5
          
find(x == 0) =
    2
    4
          
find(0 < x & x < 10*pi) =
    1
And on a matrix

M = magic(3)
          
M =
    8    1    6
    3    5    7
    4    9    2
[i,j,m] = find(M > 6)
          
i =
    1
    3
    2
          
j =
    1
    2
    3
          
m =
    1
    1
    1

See Also

<, <=,>,>=,==, ~=, isempty, nonzeros, sparse

(c) Copyright 1994 by The MathWorks, Inc.