Copy matrix and vector to avoid modifying original