Matrix Addition in Python
Here you will learn that how to do Matrix Addition in Python programming.
Matrix addition in Python refers to the operation of adding two matrices together element-wise. This means that the corresponding elements from each matrix are added together to form a new matrix with the same dimensions.
For exam.
Matrix1 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Matrix2 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Matrix_Sum = [[2, 4, 6],
[8, 10, 12],
[14, 16, 18]]
Program Code of Matrix Addition in Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #Add Two Matrices in Python # matrix 1 X = [[1,2,3], [4,5,6], [7,8,9]] # matrix 2 Y = [[1,2,3], [4,5,6], [7,8,9]] # Matrix Sum result = [[0,0,0], [0,0,0], [0,0,0]] for i in range(len(X)): for j in range(len(X[0])): result[i][j] = X[i][j] + Y[i][j] for r in result: print(r) |
Output
[2, 4, 6]
[8, 10, 12]
[14, 16, 18]
Check out our other Python examples