Matrix Addition in Python

Source code :

x=[[1,2],
    [2,4]]
y=[[3,3],
    [22,33]]
z=[[0,0],
    [0,0]]
for i in range(len(x)):
    for j in range(len(x[0])):
        z[i][j] = x[i][j] + y[i][j]
for res in z:
    print (res)

Output :

[4, 5]
[24, 37]

Algorithm :

  1. Declare 2 matrix with rows and columns
  2. Declare 3rd matrix to store the result
  3. Use for loop to define the length of the matrix
  4. Add the two matrix and store the result in 3rd matrix
  5. Finally print the result


Output :


                

Comments :