Add two matrix in Numpy Python by R4R Team

Task-
-Addition of two matrix
For sum up two matrix , there shape(order) must be same.

Example-
first matrix is :
[[2 2]
[1 1]]

second matrix is :
[[1 0]
[0 1]]

Then Addition of both matrix is :
[[3 2]
[1 2]]


program-

#import numpy
import numpy as np

l=[[1,2,3],[4,5,1],[1,1,1]]
arr1=np.array(l)
print("nMatrix1 is :n",arr1)

l=[[1,2,3],[0,0,0],[1,1,0]]
arr2=np.array(l)
print("nMatrix 2 is :n",arr2)

print("nAddition of both matrix is n",arr1+arr2)


output-

Matrix1 is :
[[1 2 3]
[4 5 1]
[1 1 1]]

Matrix 2 is :
[[1 2 3]
[0 0 0]
[1 1 0]]

Addition of both matrix is
[[2 4 6]
[4 5 1]
[2 2 1]]

-In this program, we add two 3x3 matrix




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!