分别创建一个6行6列的矩阵,计算这两个矩阵的相加、乘积,求两个矩阵的秩。
时间: 2023-05-14 18:07:19 浏览: 94
C#实现矩阵加减乘除以及求逆
4星 · 用户满意度95%
好的,以下是您所需的代码:
矩阵相加:
```python
matrix1 = [[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30],
[31, 32, 33, 34, 35, 36]]
matrix2 = [[36, 35, 34, 33, 32, 31],
[30, 29, 28, 27, 26, 25],
[24, 23, 22, 21, 20, 19],
[18, 17, 16, 15, 14, 13],
[12, 11, 10, 9, 8, 7],
[6, 5, 4, 3, 2, 1]]
result = [[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
result[i][j] = matrix1[i][j] + matrix2[i][j]
print("矩阵相加的结果:")
for r in result:
print(r)
```
矩阵乘积:
```python
matrix1 = [[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30],
[31, 32, 33, 34, 35, 36]]
matrix2 = [[36, 35, 34, 33, 32, 31],
[30, 29, 28, 27, 26, 25],
[24, 23, 22, 21, 20, 19],
[18, 17, 16, 15, 14, 13],
[12, 11, 10, 9, 8, 7],
[6, 5, 4, 3, 2, 1]]
result = [[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]
print("矩阵乘积的结果:")
for r in result:
print(r)
```
矩阵秩:
```python
import numpy as np
matrix1 = [[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24],
[25, 26, 27, 28, 29, 30],
[31, 32, 33, 34, 35, 36]]
matrix2 = [[36, 35, 34, 33, 32, 31],
[30, 29, 28, 27, 26, 25],
[24, 23, 22, 21, 20, 19],
[18, 17, 16, 15, 14, 13],
[12, 11, 10, 9, 8, 7],
[6, 5, 4, 3, 2, 1]]
matrix = np.concatenate((matrix1, matrix2), axis=0)
rank = np.linalg.matrix_rank(matrix)
print("矩阵的秩为:", rank)
```
希望这些代码能够帮助到您!
阅读全文