Matlab如何输入1到9九个数生成三阶矩阵,要求都用到
时间: 2023-05-18 20:03:41 浏览: 90
reshape 函数?
可以使用以下代码实现:
A = 1:9;
B = reshape(A, [3,3]);
其中,A 是包含 1 到 9 的数组,reshape 函数将其转换为一个 3x3 的矩阵 B。
相关问题
matlab输入一个数n,生成n阶的Hilbert矩阵
可以使用以下代码生成n阶的Hilbert矩阵:
```matlab
function H = hilbert_mat(n)
% 生成n阶的Hilbert矩阵
H = zeros(n);
for i = 1:n
for j = 1:n
H(i,j) = 1/(i+j-1);
end
end
end
```
可以通过调用 `hilbert_mat(n)` 函数来生成n阶的Hilbert矩阵。例如,要生成4阶的Hilbert矩阵,可以输入以下命令:
```matlab
hilbert_mat(4)
```
输出结果为:
```
ans =
1 0.5000 0.3333 0.2500
0.5000 0.3333 0.2500 0.2000
0.3333 0.2500 0.2000 0.1667
0.2500 0.2000 0.1667 0.1429
```
Matlab如何输入1到9随机四阶矩阵
您可以使用Matlab中的randperm函数来生成1到9的随机排列,然后使用reshape函数将其转换为四阶矩阵。具体代码如下:
rand_nums = randperm(9);
four_order_matrix = reshape(rand_nums, [4, 4]);