function a = vector_lex_unrank ( d, b, rank )

%*****************************************************************************80
%
%% vector_lex_unrank() unranks a vector using lexicographic order.
%
%  Example:
%
%    d = 3,
%    b = 3
%    RANK = 12
%
%    A = ( 2, 1, 3 )
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    31 October 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer d: the vector dimension.
%
%    integer b, the upper limit for the array indices.
%    The lower limit is 1.
%
%    integer RANK, the rank of the desired index vector.
%
%  Output:
%
%    integer A(d), the index vector of the given rank.
%
  a = zeros ( 1, d );
%
%  The rank might be too small.
%
  if ( rank < 1 )
    return
  end

  range = b^d;
%
%  The rank might be too large.
%
  if ( range < rank )
    return
  end

  k = rank - 1;
  for i = 1 : d
    range = floor ( range / b );
    j = floor ( k / range );
    a(i) = j + 1;
    k = k - j * range;
  end

  return
end
