Matrix Addition | Linear Algebra using Python

Linear Algebra using Python | Matrix Addition: Here, we are going to learn how to add two matrices in Python?
Submitted by Anuj Singh, on May 14, 2020

In the python code, we will add two Matrices. We can add two Matrices only and only if both the matrices have the same dimensions. For our code, we consider the matrices with dimensions 4x3.

Python code for matrix addition

# Linear Algebra Learning Sequence
# Matrix Addition

import numpy as np

# Use of np.array() to define a matrix
V1 = np.array([[1,2,3],[2,3,5],[3,6,8],[323,623,823]])

V2 = np.array([[965,2413,78],[223,356,500],[312,66,78],[42,42,42]])

print("--The Matrixa A-- \n", V1)
print("--The Matrix B-- \n", V2)

sem = V1 + V2

print("\n---A + B--- \n", sem)

Output:

--The Matrixa A-- 
 [[  1   2   3]
 [  2   3   5]
 [  3   6   8]
 [323 623 823]]
--The Matrix B-- 
 [[ 965 2413   78]
 [ 223  356  500]
 [ 312   66   78]
 [  42   42   42]]

---A + B--- 
 [[ 966 2415   81]
 [ 225  359  505]
 [ 315   72   86]
 [ 365  665  865]]






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.