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.