- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex44.py
More file actions
Latest commit
26 lines (20 loc) · 671 Bytes
/
Copy pathex44.py
File metadata and controls
26 lines (20 loc) · 671 Bytes
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
'''
两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵:
(改为任意行列矩阵相加)
'''
importnumpyasnp
defmatrix_add(matrix01, matrix02):
m=len(matrix01[:][0])
n=len(matrix01[0][:])
matrix=np.zeros([m,n])
foriinrange(m):
forjinrange(n):
matrix[i][j] =matrix01[i][j] +matrix02[i][j]
returnmatrix
matrix_a=np.random.random([3, 3])
matrix_b=np.random.random([3, 3])
matrix_c=matrix_add(matrix_a, matrix_b)
print(matrix_c)
# 验证
print("用numpy中的add方法做验证:")
print(np.add(matrix_a, matrix_b) ==matrix_c)