function [ data_l, data_r ] = data_bracket ( node_num, node_x, data_num, data_x )

%*****************************************************************************80
%
%% data_bracket brackets data between successive entries of a sorted vector.
%
%  Discussion:
%
%    If a data item lies outside the interval, the bracket indices are -1.
%    If a data item exactly matches a node value, both bracket indices
%    are the index of the node value.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    04 May 2019
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer node_num, the number of finite element nodes.
%
%    Input, real node_x(node_num), the x coordinates of the finite element
%    nodes, in ascending order.
%
%    Input, integer data_num, the number of data values.
%
%    Input, real data_x(data_num), the x coordinates of the data values.
%
%    Output, integer data_l(data_num), data_r(data_num), the indices of
%    finite element nodes to the immediate left and right of the data.
%    If the data is outside the interval, these indices are both set to -1.
%    If the data coincides with a finite element node, the indices are equal.
%

%
%  Treat the input as column vectors.
%
  node_x = node_x(:);
  data_x = data_x(:);
%
%  Create column vectors for the output.
%
  data_l = zeros ( data_num, 1 );
  data_r = zeros ( data_num, 1 );
%
%  Binary search for the brackets of each data value.
%
  for i = 1 : data_num

    if ( data_x(i) < node_x(1) || node_x(node_num) < data_x(i) )

      data_l(i) = -1;
      data_r(i) = -1;

    else

      l = 1;
      r = node_num;

      while ( l + 1 < r )
        m = floor ( ( l + r ) / 2 );
        if ( data_x(i) < node_x(m) )
          r = m;
        else
          l = m;
        end
      end

      if ( data_x(i) == node_x(l) )
        data_l(i) = l;
        data_r(i) = l;
      elseif ( data_x(i) == node_x(r) )
        data_l(i) = r;
        data_r(i) = r;
      else
        data_l(i) = l;
        data_r(i) = r;
      end

    end

  end

  

  return
end
