function Ak = rank_k_approximation(A,k)
  %
  % Compute the rank-k approximation to A from the ``k`` largest
  % singular values of A.
  %
  % INPUT:
  %
  %   - ``A`` -- The matrix to approximate.
  %
  %   - ``k`` -- The rank of the resulting matrix; i.e. the number
  %              of (large) singular values to keep.
  %
  % OUTPUT:
  %
  %   - ``Ak`` -- The rank-k approximation to ``A``.
  %
  [m,n] = size(A);

  if (k >= min(m,n))
    % We're keeping greater than or equal to the maximum number of
    % singular values that can exist.
    Ak = A;
    return;
  end

  [U, S, V] = svds(A, k);
  Ak = U*S*V';
end
