Added a function to free memory usage of matrix types