计算两个矩阵的 Hadamard 乘积。要求输入矩阵的行/列数(在这里假设我们使用的是 n × n 的矩阵)。
#!/usr/bin/env python3
n = int(input("Enter the value of n: "))
print("Enter values for the Matrix A")
a = []
for i in range(n):
a.append([int(x) for x in input().split()])
print("Enter values for the Matrix B")
b = []
for i in range(n):
b.append([int(x) for x in input().split()])
c = []
for i in range(n):
c.append([a[i][j] * b[i][j] for j in range(n)])
print("After matrix multiplication")
print("-" * 7 * n)
for x in c:
for y in x:
print(str(y).rjust(5), end=' ')
print()
print("-" * 7 * n)
运行如下:
shiyanlou:~/ $ ./matrixmul.py [23:45:24]
Enter the value of n: 4
Enter values for the Matrix A
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Enter values for the Matrix B
16 15 14 13
12 11 10 9
8 7 6 5