A Complete Guide To Matrix Addition in Python

# A Complete Guide To Matrix Addition in Python

Last updated:
20th Jan, 2023
Views
4 Mins
View All

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

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

 Executive Post Graduate Programme in Data Science from IIITB Professional Certificate Program in Data Science for Business Decision Making Master of Science in Data Science from University of Arizona Advanced Certificate Programme in Data Science from IIITB Professional Certificate Program in Data Science and Business Analytics from University of Maryland 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

 Data Science Career Path: A Comprehensive Career Guide Data Science Career Growth: The Future of Work is here Why is Data Science Important? 8 Ways Data Science Brings Value to the Business Relevance of Data Science for Managers The Ultimate Data Science Cheat Sheet Every Data Scientists Should Have Top 6 Reasons Why You Should Become a Data Scientist A Day in the Life of Data Scientist: What do they do? Myth Busted: Data Science doesn’t need Coding Business Intelligence vs Data Science: What are the differences?

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.

#### Rohit Sharma

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

Select Course
Select
By clicking 'Submit' you Agree to

## Suggested Blogs

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

19 Feb 2024

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

19 Feb 2024

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

19 Feb 2024

44250
Python is one of the top choices in programming languages among professionals worldwide. Its straightforward syntax allows software developers and dat

by Hemant

19 Feb 2024

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

19 Feb 2024

78142
Its intuitive syntax, extensive libraries and versatile integration capabilities have fueled incredible growth across web development and scientific c

19 Feb 2024

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

19 Feb 2024

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