Creating the matrix factorization function