A Python program to perform arithmetic operations on two matrices.
To represent a matrix we use the concept of nested lists. The elements in both the input matrices are represented as linked lists. To store the result the output list is initialized to zero. When we iterate the two matrices (Here M1 and M2 are the two chosen matrices) by specific operation then the result of the corresponding elements in the matrices are assigned to the respected positions in the output matrix. Finally we will be displayed the desired output. To perform arithmetic operations like +,-,* and / the number of elements in both the matrices or length of both the matrices should be equal.
For 2X2 matrix
#Addition of two matrices
We can also write the same above code as follows; not only for addition but also for subtraction, multiplication and division operations. But we will get same output for both formats.
Output
[4, 4]
[5, 3] |
#Subtraction of two matrices
Output
[-2, 0]
[3, -1] |
#Multiplication of two matrices
Output
[3, 4]
[4, 2] |
#Division of two matrices
Output
[1.0, 1.0]
[4.0, 0.5] |
For 3×3 matrix
#Addition of two matrices
Output
[2, 3, 4]
[5, 6, 7] [8, 9, 10] |
Task
Let you try the following:
1. Write python scripts to perform the remaining operations like subtraction, multiplication and division on 3×3 matrices.