refine the matrix