A numerical method for evaluating matrix elements of Hermitian operators