Updated code. Create two functions for matric caching.
Verified and tested using 2x2, 3x3, 4x4 matrix.