cpp version to compute error matrix