matrix memory allocation