Python Program To Add Two Matrices Using Numpy

Python Program To Add Two Matrices Using Numpy

In this tutorial, you will learn to write a Python Program To Add Two Matrices Using Numpy.

NumPy is a Python library used for scientific computing and data analysis.

It provides support for multidimensional arrays, matrices, and high-level mathematical functions to operate on these arrays.

NumPy is a powerful library for numerical computations in Python.

It is widely used in the fields of data science, machine learning, and artificial intelligence.

NumPy arrays are similar to Python lists, but they allow for fast, element-wise operations and efficient handling of large data sets.

To understand this program you should have an understanding of the following topics:

  1. Numpy

Python Program To Add Two Matrices Using Numpy

import numpy as np

# Define two matrices
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
B = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])

# Add the two matrices
C = A + B

# Print the result
print("Matrix A:\n", A)
print("Matrix B:\n", B)
print("Matrix C = A + B:\n", C)

Output

Matrix A:
[[1 2 3]
[4 5 6]
[7 8 9]]
Matrix B:
[[9 8 7]
[6 5 4]
[3 2 1]]
Matrix C = A + B:
[[10 10 10]
[10 10 10]
[10 10 10]]

In this program, we first import the NumPy library.

We then define two matrices A and B using NumPy arrays.

We add the two matrices using the + operator and store the result in the variable C.

Finally, we print out the three matrices to verify the result.

Note that the matrices must have the same dimensions for addition to be valid.


Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

Subscribe now to keep reading and get access to the full archive.

Continue reading