Function Reference
— Loadable Function: find (x)
— Loadable Function: find (x, n)
— Loadable Function: find (x, n, direction)

Return a vector of indices of nonzero elements of a matrix, as a row if x is a row or as a column otherwise. To obtain a single index for each matrix element, Octave pretends that the columns of a matrix form one long vector (like Fortran arrays are stored). For example,

          find (eye (2))
               [ 1; 4 ]

If two outputs are requested, find returns the row and column indices of nonzero elements of a matrix. For example,

          [i, j] = find (2 * eye (2))
               i = [ 1; 2 ]
               j = [ 1; 2 ]

If three outputs are requested, find also returns a vector containing the nonzero values. For example,

          [i, j, v] = find (3 * eye (2))
               i = [ 1; 2 ]
               j = [ 1; 2 ]
               v = [ 3; 3 ]

If two inputs are given, n indicates the number of elements to find from the beginning of the matrix or vector.

If three inputs are given, direction should be one of "first" or "last" indicating that it should start counting found elements from the first or last element.

Note that this function is particularly useful for sparse matrices, as it extracts the non-zero elements as vectors, which can then be used to create the original matrix. For example,

          sz = size(a);
          [i, j, v] = find (a);
          b = sparse(i, j, v, sz(1), sz(2));

See also: sparse