Coerces a regular matrix to a matrix with type-safe dimensions and throws an error if coercion failed