Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconA Complete Guide To Matrix Addition in Python

A Complete Guide To Matrix Addition in Python

Last updated:
20th Jan, 2023
Views
Read Time
4 Mins
share image icon
In this article
Chevron in toc
View All
A Complete Guide To Matrix Addition in Python

Python is a language that holds the grounds for performing various operations. In this article, we are going to take an in-depth look into matrix addition in Python.

A matrix is defined as a rectangular representation of an array of symbols, numbers or other object representation, which is expressed using rows and columns. For instance, let us take a matrix P which is a 3*3 matrix. It can be represented as follows:

In mathematics, a matrix is nothing but an array of symbols, numbers or expressions arranged in the form of rows and columns and represented in a rectangular form. For example: Let us take a 2*3 matrix A. It is depicted as follows:

                 2     4     7

        A=       3     5     9

                 6     1     8

Various operations like addition, subtraction, division etc., can be performed on these matrices. Let us now take a deeper insight into matrix addition in Python.

Check Out upGrad’s Data Science Courses 

Matrix Addition in Python

In this section, we are going to look at and understand how matrix addition in Python works and what are the various methods of doing so.

Similar to any other type of addition, adding up the elements of one matrix to that of another is known as matrix addition. For eg., if elements of matrix A are added to the elements of matrix B, then matrix C would store the result of the addition i.e., C= A+B.

In Python, matrix addition can be performed only on matrices with the same shape, i.e., if A is a 2*3 matrix, then it can be added with the matrix B, which is also 2*3 but not with C that is a 3*3 matrix.

Another important note to keep in mind about matrix addition in Python is that in this particular language, the flow of addition is only one-way. It implies that the first element of matrix A[1,1] can only be added to the first element of matrix B[1,1]

Explore our Popular Data Science Courses

Let us take an example to understand the basic matrix addition in Python before moving on to other methods.

               2     3     4                 1     1      1                             

    A=         1     5     8       B=        2      2     2

               7    6      9                 1      1     1


                             3     4    5

         C =  A+B =          3     7    10

                             8     7    10         

Various Methods of Matrix Addition in Python

There are 3 basic methods of adding matrices in Python. Let us understand each of them with illustrative examples:

  • Matrix Addition Utilizing Nested List Comprehension

One of the most wonderful characteristics of Python is List Comprehensions which is defined as an intelligent method of iterating on an iterable object in order to create lists. Similar to nested loops, nested list comprehension is a list comprehension nested inside another.

Using this, matrices can be implemented as a nested list. 

Read our popular Data Science Articles

Following is an example for better understanding:

Eg.

#program for adding two matrices via list comprehension                      

                 A= [ [2, 3, 4], [1, 5, 8], [7, 6, 9] ]

                 B= [ [4, 2, 2], [1, 4, 1], [2, 2, 4] ]

                 Output=[  [A[i][j] + B[i][j] for  j  in range(len(A[0])) ] for  i  in range (len(A)) ]

                 For r in output:

                 Print(r)

#OUTPUT:  [ [6, 5, 6], [2, 9, 9], [9, 8, 13] ]

 

  • Matrix Addition Utilizing Nested Loops

As we all know, nested loops are loops inside loops. In the case of matrix addition in Python, nested loops iterate through every column & row and after each loop of iteration, the respective elements of the matrices are added and are stored in a third matrix. 

Eg.

#program to add two matrices using nested loops

                 A= [ [2, 3, 4], [1, 5, 8], [7, 6, 9] ]

                 B= [ [2, 1, 2], [1, 2, 1], [2, 3, 2] ]

                    0    0    0

  Output=           0    0    0

                    0    0    0 

#iterate through rows

for i in range (len(A)):

#iterate through columns

for j in range (len(A[0])):

output[i][j]= A[i][j] + B[i][j]

for r in output:

print(r)

#OUTPUT:  [ [4, 4, 6], [2, 7, 9], [9, 9, 11] ]

 

  • Matrix Addition Utilizing Sum & Zip() Function

The zip() function in Python basically accepts the iterator of the matrix’s every element and then maps them and adds them via the sum() function. 

Eg.

#program for adding two matrices via sum & zip()                         

                  A= [ [2, 3, 4], [1, 5, 8], [7, 6, 9] ]

                  B= [ [2, 2, 1], [1, 1, 2], [1, 2, 2] ]

                Output = [map (sum, zip(*i) ) for i in zip( A, B) ]

                Print (output)

          #OUTPUT:  [ [4, 5, 5], [2, 6, 10], [8, 8, 11] ]

 

Conclusion

Amongst all the different methods of matrix addition in Python explained above, any of them can be used according to your requirement & convenience. However, list comprehension is one of the easiest and preferred methods, owing to its accuracy.

If you are curious to learn about tableau, data science, check out IIIT-B & upGrad’s Executive PG Programme in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

Explore Free Courses

Suggested Blogs

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
78147
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

19 Feb 2024

Sorting in Data Structure: Categories & Types [With Examples]
135433
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e
Read More

by Rohit Sharma

19 Feb 2024

Data Science Vs Data Analytics: Difference Between Data Science and Data Analytics
66850
Summary: In this article, you will learn, Difference between Data Science and Data Analytics Job roles Skills Career perspectives Which one is right
Read More

by Rohit Sharma

19 Feb 2024

13 Exciting Python Projects on Github You Should Try Today [2023]
44250
Python is one of the top choices in programming languages among professionals worldwide. Its straightforward syntax allows software developers and dat
Read More

by Hemant

19 Feb 2024

Top 15 Python AI & Machine Learning Open Source Projects
35842
Machine learning and artificial intelligence are some of the most advanced topics to learn. So you must employ the best learning methods to make sure
Read More

by Pavan Vadapalli

19 Feb 2024

Top 21 Python Developer Skills You Must Need To Become a Successful Python Developer
78142
Its intuitive syntax, extensive libraries and versatile integration capabilities have fueled incredible growth across web development and scientific c
Read More

by Rohit Sharma

19 Feb 2024

Most Frequently Asked NumPy Interview Questions and Answers [For Freshers]
28513
If you are looking to have a glorious career in the technological sphere, you already know that a qualification in NumPy is one of the most sought-aft
Read More

by Rohit Sharma

19 Feb 2024

Top 30 Python Pattern Programs You Must Know About
33195
Summary Pattern in Python or “Python patterns” is an essential part of Python programming, especially when you are just starting out with using algor
Read More

by Rohit Sharma

19 Feb 2024

Top 12 Fascinating Python Applications in Real-World [2024]
154551
It is a well-established fact that Python is one of the most popular programming languages in both the coding and Data Science communities. But have y
Read More

by Rohit Sharma

19 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon