在 MATLAB 中,有一个叫做 reshape 的便利函数,它可以将一个m x n矩阵改成一个新的矩阵,并保持其原始数据.你得到一个m x n矩阵矩阵和两个整数r和c,代表所需的重构矩阵的行数和列数.重构矩阵应该填满原始矩阵的所有元素,以相同的行顺序.如果使用给定参数进行重构操作是可能的和合法的,输出新的重构矩阵;否则输出原始矩阵.
Here is a possible solution to your problem in MATLAB:

```
function res = matrixReshape(mat, r, c)
% Check if reshape is possible
[m, n] = size(mat);
if r*c ~= m*n
    res = mat;
    return
end
% Reshape the matrix
res = reshape(mat, r, c)';
end
```

You can call this function with your input matrix mat and the desired number of rows and columns r and c. If the reshape is possible and legal, it will return the new reshaped matrix; otherwise, it will return the original matrix.

Note that the reshape function in MATLAB returns the reshaped matrix in column-major order, so we use the transpose operator `'` to convert it back to row-major order.