Posts

Showing posts with the label Python program to add two matrices

Python program to add two matrices

Image
Addition of two matrices in python Problem description : The program gets two matrices as input from the user and returns the sum of two matrices. For adding two matrices the input matrices should be of same order. Example,          1 2 3 A=    4 5 6           Order of A => 3x3          7 8 9          1 1 1 B=    2 2 2             Order of B => 3x3          3 3 3               2   3   4 A+B=   6   7   8               10 11 12 Whereas fo the matrices,            1 2 3 A=     4 5 6               Order of A => 3x3           7 8 9          1 2 B=    3 5                    Order of B => 3x2          6 7 Here the addition of two matrices is not possible as the order of the input matrices A and B are not same. Prerequisite :  Getting matrix input from user Procedure : 1. Start. 2. Get two matrices from user, say m1 and m2. 3. Create an empty list, result matrix. 4. loop (0 to row length)     => create an empty list, l.     =>  loop (0 to column length)           => add