function unique_index = i4col_unique_index ( m, n, a )

%*****************************************************************************80
%
%% I4COL_FIRST_INDEX indexes the first occurrence of values in an I4COL.
%
%  Discussion:
%
%    An I4COL is an M by N array of I4 values.
%    It is regarded as an array of N columns 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:
%
%    24 August 2008
%
%  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".
%
%    integer A(M,N), the array.
%
%  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
        if ( all ( a(1:m,j1) == a(1:m,j2) ) )
          unique_index(j2) = unique_num;
        end
      end

    end

  end

  return
end
