function unique_index = r8col_tol_unique_index ( m, n, a, tol )

%*****************************************************************************80
%
%% r8col_tol_unique_index() indexes tolerably unique entries in an R8COL.
%
%  Discussion:
%
%    An R8COL is an M by N array of R8's, regarded as an array of N columns,
%    each of length M.
%
%    For element A(1:M,J) of the matrix, UNIQUE_INDEX(J) is the uniqueness index
%    of A(1:M,J).  That is, if A_UNIQUE contains the unique elements of A, 
%    gathered in order, then 
%
%      A_UNIQUE ( 1:M, UNIQUE_INDEX(J) ) = A(1:M,J)
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    17 July 2010
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer M, N, the number of rows and columns of A.
%    The length of an "element" of A, and the number of "elements".
%
%    real A(M,N), the array.
%
%    real TOL, a tolerance for equality.
%
%  Output:
%
%    integer UNIQUE_INDEX(N), the unique index.
%
  unique_index(1:n) = -1;
  unique_num = 0;

  for j1 = 1 : n

    if ( unique_index(j1) == -1 )

      unique_num = unique_num + 1;
      unique_index(j1) = unique_num;

      for j2 = j1 + 1 : n
        diff = max ( abs ( a(1:m,j1) - a(1:m,j2) ) );
        if ( diff <= tol )
          unique_index(j2) = unique_num;
        end
      end

    end

  end

  return
end
