Python Matrix Addition

Contents

Python – Matrix Addition

In Python, a Matrix can be represented using a nested list. Lists inside the list are the rows.

Following is a simple example of nested list which could be considered as a 2x3 matrix.

``matrixA = [ [2, 8, 4], [3, 1, 5] ]``

The two lists inside matrixA are the rows of the matrix. Number of elements inside a row represent the number of columns.

Also, in Python programming, the indexing start from 0. So, when we specify matrixA[2][4] in the program, that is actually [2+1][4+1] = [3][5], element of third row and fifth column.

As we are using Lists to represent matrices, you should be careful with the results of arithmetic operator for two lists.

For example, in the following statement, the result of addition operator does not return a list of element by element addition of the two operands, but returns a list with the inner lists of second operand appended to the elements of first operand.

``matrixC = matrixA + matrixB // where these are nested lists``

To compute the addition of two matrices, which are list of lists in Python, you have to traverse through the rows and columns, and compute the addition.

In this tutorial, we will learn how to do Matrix Addition in Python using Lists.

Examples

1. Add two matrices using For Loop

In this example, we shall take two matrices and compute their addition. We shall use Python For Loop to traverse through all the elements of a matrix.

Python Program

``````matrixA = [ [2, 1, 3], [3, 1, 5] ]
matrixB = [ [1, 4, 2], [5, 2, 0] ]

# Matrix where we shall store our result

# Intialize this to 0, with size as that of matrixA or matrixB
matrixC = [[0 for i in range(len(matrixA[0]))] for j in range(len(matrixA))]

# For each row in the matrix
for i in range(len(matrixB)):
#for each element in the row
for j in range(len(matrixB[0])):
matrixC[i][j] = matrixA[i][j] + matrixB[i][j]

print('  matrixA:', matrixA)
print('+ matrixB:', matrixB)
print('-------------------------------')
print('  matrixC:', matrixC)``````
Run Code Copy

Output

``````  matrixA: [[2, 1, 3], [3, 1, 5]]
+ matrixB: [[1, 4, 2], [5, 2, 0]]
-------------------------------
matrixC: [[3, 5, 5], [8, 3, 5]]``````

2. Add two matrices using List Comprehension

In this example, we shall take two matrices and compute their addition. We shall use Python List Comprehension to compute addition at element level.

Python Program

``````matrixA = [ [2, 1, 3], [3, 1, 5] ]
matrixB = [ [1, 4, 2], [5, 2, 0] ]

matrixC = [[matrixA[i][j] + matrixB[i][j]  for j in range(len(matrixA[0]))] for i in range(len(matrixA))]

print('  matrixA:', matrixA)
print('+ matrixB:', matrixB)
print('-------------------------------')
print('  matrixC:', matrixC)``````
Run Code Copy

The code for addition of matrices using List Comprehension is very concise.

Output

``````  matrixA: [[2, 1, 3], [3, 1, 5]]
+ matrixB: [[1, 4, 2], [5, 2, 0]]
-------------------------------
matrixC: [[3, 5, 5], [8, 3, 5]]``````

Summary

In this tutorial of Python Examples, we learned how to do Matrix Addition in Python using For loop and List comprehension, with the help of well detailed examples.

Related Tutorials

Code copied to clipboard successfully 👍