Linear recurrence relations in the algebra of matrices and applications