updating residual matrices conditionally