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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.