how to add two matrices in python

Solutions on MaxInterview for how to add two matrices in python by the best coders in the world

showing results for - "how to add two matrices in python"
Bruno
07 Oct 2016
1num = [2, 3, 4], [5, 6, 7]
2num2 = [8, 9, 10], [11, 12, 13]
3num3 = [0, 0, 0], [0, 0, 0]
4for i in range(len(num)):
5    print(num[i])
6for i in range(len(num)):
7    print(num2[i])
8print()
9for i in range (len(num)):
10    for j in range(len(num)+1):
11        num3[i][j] = num[i][j] + num2[i][j]
12
13for i in range(len(num3)):
14    print(num3[i])
Trenton
09 Apr 2017
1# Program to add two matrices using nested loop
2
3X = [[12,7,3],
4    [4 ,5,6],
5    [7 ,8,9]]
6
7Y = [[5,8,1],
8    [6,7,3],
9    [4,5,9]]
10
11result = [[0,0,0],
12         [0,0,0],
13         [0,0,0]]
14
15# iterate through rows
16for i in range(len(X)):
17   # iterate through columns
18   for j in range(len(X[0])):
19       result[i][j] = X[i][j] + Y[i][j]
20
21for r in result:
22   print(r)
23
Moritz
05 Nov 2018
1matrix1 = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
2matrix2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
3
4
5def addTheMatrix(matrix1, matrix2):
6    matrix1Rows = len(matrix1)
7    matrix2Rows = len(matrix2)
8    matrix1Col = len(matrix1[0])
9    matrix2Col = len(matrix2[0])
10
11    #base case
12    if(matrix1Rows != matrix2Rows or matrix1Col != matrix2Col):
13        return "ERROR: dimensions of the two arrays must be the same"
14
15    #make a matrix of the same size as matrix 1 and matrix 2
16    matrix = []
17    rows = []
18
19    for i in range(0, matrix1Rows):
20        for j in range(0, matrix2Col):
21            rows.append(0)
22        matrix.append(rows.copy())
23        rows = []
24
25    #loop through the two matricies and the summation should be placed in the
26    #matrix
27    for i in range(0, matrix1Rows):
28        for j in range(0, matrix2Col):
29            matrix[i][j] = matrix1[i][j] + matrix2[i][j]
30            
31    return matrix
32
33
34
35print(addTheMatrix(matrix1, matrix2)) 
36#output = [[1, 3, 5], [7, 9, 11], [13, 15, 17]]
Santiago
31 Nov 2019
1# Program to add two matrices using list comprehension
2
3X = [[12,7,3],
4    [4 ,5,6],
5    [7 ,8,9]]
6
7Y = [[5,8,1],
8    [6,7,3],
9    [4,5,9]]
10
11result = [[X[i][j] + Y[i][j]  for j in range(len(X[0]))] for i in range(len(X))]
12
13for r in result:
14   print(r)
queries leading to this page
add two large matrix in pythonhow to add two matricx in pythonmatrices addition in pythonaddition of two matrices in pythonwrite a program to add two matrices pythonaddition of 2 matrices pythonmatrix addition pythonsum of two matrix in pythonappend 2 matrices in pythonwrite a program for addition of two matrices of the same dimensions in pythonadd two matrix pythonmatrix addition in python with numpypython program to add two matricespython sum two matricesadd matrizces inpythonpython add two matricesadd two matrices in pythonsum of two matrices in pythonadd of two matrix in pythonwrite a program to add two matrices python 3x3python multiply two matriceswrite a program for the addition of two matrices in pythonsum two matrices pythonpython matrices additionadding two matrices together numpysum two matrix pythonhow to join two matrices in pythonaddition of two big matrices in pythonwrite a python program to add two matrices from userwrite a python program for addition of two matricesaddition of matriz in pythonhow to add matrix in pythonadd two matrices in python numpywrite a python program to add two matrices 3fwrite a python program to add 2 matriceshow to sum two numpy matrices with different shapesaddition of two 3 2a3 matrix in pythonmatrix sum in pythonhow to traverse list to to do matrix addition for 3x3write a python program to add two matrix program to add two matrices in pythoncreate a function that adds and subtract matrix in one pythonadd two 2d arrays in python using listssum of 2 matrix in pythonpython adding matriceshow to put together 2 matrices in python using numpyhow to add two matrix in oythona function to calculate summation of 2 matrices pythonhow to add two matrix in pythonhow to combine two matrices in pythonjoin two matrices pythonhow to add two matrices in numpypython matrix additionpython list matrix addsum of 2 matrices using pythonadd 2 matrix in pythonpython program to add two matrix how to sum 2d two matrices in pythontwo matrix addition in pythonconcatenating two matrices in pythonaddition and multiplication of two matrices in pythonwrite a python program to implement addition of two matrices write function that adds 2 matrices 2f 2d lists and returns as listhow to sum two matrices in pythonprogram to add 2 matrices in pythonaddition of matrix in pythondirect addition of 2 matrices in pythonhow to add matricies in pythonsum of matrix in pythonprgram to perform mrtaix taddition in data structure in pythonhow to add two matrices in pythoncombining matrices using pythonaddition multiplication and substraction of two matix in pyhow to add two matrices with two different demensions in pythonput two matrices together pythonadd two matrix n 2am in pythonaddition of two matrix in pythonfunction to add matrix pythonhow to concatenate 2 matrices in pythonfunction adding matrices pythonhow to combine matrices pythonadd matrix pythonprogram to add two matrices in python with examplessum 2 matrix pythonhow add matrices numbers in pythonwrite a program to find the sum of two 3x3 matrices entered by the user pythonhow to add matrices in pythonhow to append two matrices in pythonpython add matrixes2 matrices 282d lists 29 add together pythonwrite a python program for addition of two matrices x 2c yadding two matrices in pythonfind the sum of two matrix after input numpy write function that adds 2 matrices 2f 2d lists and returns the result in pythonprgram to perform mrtaix addition in data structure in pythonpython program to add 2 matricesmatrix addition in numptymatrix addition using pythonhow to add two matrices in python dynamic input from useraddition of matrix pythonperform matrix addition using listsaddition of matrices in pythonmatrix addition program concept in pythonproduct of two matrices in pythonaddition of 2 matrices in pythonpython multiplying two matricesadd and subtract two matrices in pythonjoin two matrices in pythonadd two matrices in python of different dimensionsmultiplication of two matrices in pythonconcatenate two matrices pythonaddition and multiplication of matrices in pythonpython program to add matricesadding matrix in pythonhow to add 2 matrices in pythonadd two matrix 2d in pythonadd matrices pythonadd two matrix in pythonadd 2 matrices pythontwo matrics in pythonmatrix add in pythonaddition of two matriceswrite a program to add two matrices in pythonpython matrix addition using listhow to add to a matrix in pythonmatrixtsum code using pythonwrite a python program to add two matrices using listfunction that add 2 matrices in pythonadding matrices in pythonwrite a python program to add two matricespython add 2 matricesfind the sum of two matrix after input jupyter notebook5 write a python program to add two matrices using listwrite a python program to add two 3 2a3 matrices and display it how to concatenate two matrices in python1 write a python program to add two matricespython program for matrix additionfind the sum of two matrix in pythonhow to add matrix to matrix in pythonhow matrix add two python listsmatrix addition and multiplication in pythonmatrix addition operation in pythonadd 2 matrices in pythonpython program to add two matrices addition of matrix program in pythonput two matrices together in an array pythonadd two matrices pythonmatrix addition in pythonadd matrix in pythonaddition matrix in pythonhow to join 2 matrices in pythonhow to add two matrices in python