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

Priority Queue in Data Structure: Characteristics, Types & Implementation
57470
Introduction The priority queue in the data structure is an extension of the “normal” queue. It is an abstract data type that contains a
Read More

by Rohit Sharma

15 Jul 2024

An Overview of Association Rule Mining & its Applications
142458
Association Rule Mining in data mining, as the name suggests, involves discovering relationships between seemingly independent relational databases or
Read More

by Abhinav Rai

13 Jul 2024

Data Mining Techniques & Tools: Types of Data, Methods, Applications [With Examples]
101688
Why data mining techniques are important like never before? Businesses these days are collecting data at a very striking rate. The sources of this eno
Read More

by Rohit Sharma

12 Jul 2024

17 Must Read Pandas Interview Questions & Answers [For Freshers & Experienced]
58119
Pandas is a BSD-licensed and open-source Python library offering high-performance, easy-to-use data structures, and data analysis tools. The full form
Read More

by Rohit Sharma

11 Jul 2024

Top 7 Data Types of Python | Python Data Types
99373
Data types are an essential concept in the python programming language. In Python, every value has its own python data type. The classification of dat
Read More

by Rohit Sharma

11 Jul 2024

What is Decision Tree in Data Mining? Types, Real World Examples & Applications
16859
Introduction to Data Mining In its raw form, data requires efficient processing to transform into valuable information. Predicting outcomes hinges on
Read More

by Rohit Sharma

04 Jul 2024

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

04 Jul 2024

Most Common Binary Tree Interview Questions & Answers [For Freshers & Experienced]
10477
Introduction Data structures are one of the most fundamental concepts in object-oriented programming. To explain it simply, a data structure is a par
Read More

by Rohit Sharma

03 Jul 2024

Data Science Vs Data Analytics: Difference Between Data Science and Data Analytics
70274
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

02 Jul 2024

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