当前位置:网站首页>Example 044: Matrix Addition

Example 044: Matrix Addition

2022-08-10 03:32:00 lazily

Title: Calculate the addition of two matrices.

Program analysis: Create a new matrix, use for to iterate and take out the values ​​of the corresponding positions in the X and Y matrices, add them and put them in the corresponding positions of the new matrix.

I. Code

X = [[12, 7, 3],[4, 5, 6],[7, 8, 9]]Y = [[5, 8, 1],[6, 7, 3],[4, 5, 9]]res = [[0, 0, 0],[0, 0, 0],[0, 0, 0]]for i in range(len(res)):for j in range(len(res[0])):res[i][j] = X[i][j] + Y[i][j]print(res)

Second, the execution result

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Lazy laughter and sincerityInvite you to click below to learn and discuss together↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

原网站

版权声明
本文为[lazily]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208100201343542.html