function k = r8st_ij_to_k ( nz_num, row, col, i, j )

%*****************************************************************************80
%
%% r8st_ij_to_k() seeks the compressed index of the (I,J) entry of A.
%
%  Discussion:
%
%    If A(I,J) is nonzero, then its value is stored in location K.
%
%    This routine searches the r8st storage structure for the index K
%    corresponding to (I,J), returning -1 if no such entry was found.
%
%    This routine assumes that the data structure has been sorted,
%    so that the entries of ROW are ascending sorted, and that the
%    entries of COL are ascending sorted, within the group of entries
%    that have a common value of ROW.
%
%    The r8st storage format stores the row, column and value of each nonzero
%    entry of a sparse matrix.
%
%    The r8st format is used by CSPARSE ("sparse triplet"), SLAP
%    ("nonsymmetric SLAP triad"), by MATLAB, and by SPARSEKIT ("COO" format).
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    13 July 2007
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer NZ_NUM, the number of nonzero elements in
%    the matrix.
%
%    integer ROW(NZ_NUM), COL(NZ_NUM), the row and
%    column indices of the nonzero elements.
%
%    integer I, J, the row and column indices of the
%    matrix entry.
%
%  Output:
%
%    integer K, the r8st index of the (I,J) entry.
%
  lo = 1;
  hi = nz_num;

  while ( true )

    if ( hi < lo )
      k = -1;
      break
    end

    md = floor ( ( lo + hi ) / 2 );

    if ( row(md) < i | ( row(md) == i & col(md) < j ) )
      lo = md + 1;
    elseif ( i < row(md) | ( row(md) == i & j < col(md) ) )
      hi = md - 1;
    else
      k = md;
      break
    end

  end

  return
end
