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

Top 13 Highest Paying Data Science Jobs in India [A Complete Report]
905090
In this article, you will learn about Top 13 Highest Paying Data Science Jobs in India. Take a glimpse below. Data Analyst Data Scientist Machine
Read More

by Rohit Sharma

12 Apr 2024

Most Common PySpark Interview Questions & Answers [For Freshers & Experienced]
20848
Attending a PySpark interview and wondering what are all the questions and discussions you will go through? Before attending a PySpark interview, it’s
Read More

by Rohit Sharma

05 Mar 2024

Data Science for Beginners: A Comprehensive Guide
5064
Data science is an important part of many industries today. Having worked as a data scientist for several years, I have witnessed the massive amounts
Read More

by Harish K

28 Feb 2024

6 Best Data Science Institutes in 2024 (Detailed Guide)
5149
Data science training is one of the most hyped skills in today’s world. Based on my experience as a data scientist, it’s evident that we are in
Read More

by Harish K

28 Feb 2024

Data Science Course Fees: The Roadmap to Your Analytics Career
5075
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.
Read More

by Harish K

28 Feb 2024

Inheritance in Python | Python Inheritance [With Example]
17594
Python is one of the most popular programming languages. Despite a transition full of ups and downs from the Python 2 version to Python 3, the Object-
Read More

by Rohan Vats

27 Feb 2024

Data Mining Architecture: Components, Types & Techniques
10772
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a
Read More

by Rohit Sharma

27 Feb 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
80599
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]
138984
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

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