Updated cache function for matrix