fix memory allocation for large matrices