addition of two matrices in python

Solutions on MaxInterview for addition of two matrices in python by the best coders in the world

showing results for - "addition of two matrices in python"
Alexa
26 May 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])
Carl
25 Nov 2017
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]]
Michelle
04 Feb 2016
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
write a python program to add two 3 2a3 matrices and display it product of two matrices in pythonhow to add to a matrix in pythonmultiplication of two matrices in pythonhow to append two matrices in pythonpython multiplying two matricesmatrices addition in pythonaddition of two big matrices in pythonhow to concatenate two matrices in pythonwrite a program to add two matrices pythonadd 2 matrix in pythonaddition of matriz in pythonappend two matrices python examplepython add two matricesadd two matrix 2d in pythonadd two large matrix in python write function that adds 2 matrices 2f 2d lists and returns as listprgram to perform mrtaix addition in data structure in pythonadd two matrices in pythonhow to add two matrices in pythontwo matrix addition in pythonaddition and multiplication of two matrices in pythonadd 2 matrices pythonaddition and multiplication of matrices in pythonhow to traverse list to to do matrix addition for 3x3addition of 2 matrices pythonaddition of 2 matrices in pythonaddition of two matrices in pythonpython multiply two matricesadding matrices in pythonwrite a python program to add two matricesjoin two matrices pythonwrite a program to add two matrices in pythonpython add matrixesaddition matrix in pythonput two matrices together pythonmatrix addition and multiplication in pythonwrite a python program to add 2 matricespython program to add two matriceshow to add two matrices in numpypython sum two matriceswrite a python program to add two matrices 3fadd matrix pythonhow to add 2 matrices in pythonadding two matrices in pythonadding two matrices together numpyhow to add two matrices in python dynamic input from userwrite a python program to add two matrix find the sum of two matrix after input numpyprgram to perform mrtaix taddition in data structure in pythonhow to add matrix to matrix in pythonadd two matrix n 2am in pythonconcatenate two matrices python2 matrices 282d lists 29 add together pythonmatrix addition in pythonwrite a python program to implement addition of two matrices direct addition of 2 matrices in pythonaddition of matrices in pythonwrite a program for addition of two matrices of the same dimensions in pythonhow to concatenate 2 matrices in pythonadd 2 matrices in pythonsum of two matrices in pythonpython program for matrix additionhow to combine two matrices in pythonwrite a program to find the sum of two 3x3 matrices entered by the user pythonhow to add matricies in pythonappend 2 matrices in pythonprogram to add two matrices in python with exampleswrite a python program for addition of two matricesmatrix addition using pythonpython matrix additionhow to join 2 matrices in pythonhow to join two matrices in pythonadd of two matrix in pythonfunction that add 2 matrices in pythonadd two matrices pythonpython program to add 2 matricesfunction to add matrix pythonsum two matrices pythoncombining matrices using pythonprogram to add two matrices in pythonhow to add two matrix in oythonmatrix sum in pythonsum two matrix pythonadd matrix in pythona function to calculate summation of 2 matrices pythonsum of two matrix in pythonaddition multiplication and substraction of two matix in pywrite a python program for addition of two matrices x 2c ypython add 2 matricesmatrix addition operation in pythonjoin two matrices in pythonpython list matrix addput two matrices together in an array pythonconcatenating two matrices in pythonpython program to add two matrix how to combine matrices pythonpython adding matricesadd two matrices in python numpyfind the sum of two matrix in pythonhow to add two matrix in pythonhow to add matrices in pythonhow to put together 2 matrices in python using numpysum 2 matrix python1 write a python program to add two matricesadd two 2d arrays in python using listshow matrix add two python listsadd and subtract two matrices in pythonfunction adding matrices pythonprogram to add 2 matrices in pythonadd matrices pythonpython matrices addition5 write a python program to add two matrices using listhow to sum two matrices in pythonaddition of matrix program in pythonmatrix addition pythonwrite a program to add two matrices python 3x3perform matrix addition using listsmatrixtsum code using pythonwrite a python program to add two matrices using listhow add matrices numbers in pythonwrite a program for the addition of two matrices in pythonhow to add two matrices with two different demensions in pythonhow to sum 2d two matrices in pythonadd matrizces inpythonhow to add two matrices without using built in function in pythonhow to sum two numpy matrices with different shapesaddition of two matrix in pythonwrite a python program to add two matrices from usersum of 2 matrices using pythontwo matrics in pythonhow to add two matricx in pythonadd two matrix pythonadd two matrices in python of different dimensionssum of 2 matrix in pythonadd two matrix in pythonpython program to add two matrices addition of two matricesaddition of matrix in python write function that adds 2 matrices 2f 2d lists and returns the result in pythonpython matrix addition using listmatrix add in pythonfind the sum of two matrix after input jupyter notebookaddition of two 3 2a3 matrix in pythoncreate a function that adds and subtract matrix in one pythonsum of matrix in pythonaddition of two matrices in python