this is incorrect code; replace .copyTo(x_mat) with .copyTo(x);