#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
lenX = len(X)
Z = []
for i in range(lenX):
temp = []
for j in range(len(X[i])):
temp.append(X[i][j]+Y[i][j])
Z.append(temp)
print('X=',X)
print('Y=',Y)
print('Z=',Z)
测试结果:
X= [[12, 7, 3], [4, 5, 6], [7, 8, 9]]
Y= [[5, 8, 1], [6, 7, 3], [4, 5, 9]]
Z= [[17, 15, 4], [10, 12, 9], [11, 13, 18]]