Fri Jul 30 2021

Matrix Addition

File Name: matrix-addition.py

#!/usr/bin/evn python

# 2D array
x = [[1,2,3],
        [4,5,6],
        [7,8,9]]

y = [[9,8,7],
        [6,5,4],
        [3,2,1]]

mSum = [[0,0,0],
                [0,0,0],
                [0,0,0]]

# Iterate through rows
for i in range(len(x)):
    # Iterate through columns
    for j in range(len(x[0])):
        mSum[i][j] = x[i][j] + y[i][j]

for mxs in mSum:
    print(mxs)


# ***** Output *****
# [10, 10, 10]
# [10, 10, 10]
# [10, 10, 10]

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.