Followers

Showing posts with label Sum of Matrices. Show all posts
Showing posts with label Sum of Matrices. Show all posts

Thursday, April 27, 2023

A python program to calculate the sum of two matrices.


Here's a Python program that prompts the user for the order and elements of two 2D matrices, calculates their sum, and displays the matrices and their sum:

Python
# prompt user for order of matrices rows1 = int(input("Enter number of rows for matrix 1: ")) cols1 = int(input("Enter number of columns for matrix 1: ")) rows2 = int(input("Enter number of rows for matrix 2: ")) cols2 = int(input("Enter number of columns for matrix 2: ")) # validate that matrices can be added if rows1 != rows2 or cols1 != cols2: print("Error: matrices cannot be added because they have different dimensions.") exit() # initialize matrices matrixA = [] matrixB = [] sum_matrix = [] # prompt user for elements of first matrix print("Enter elements of matrix 1:") for i in range(rows1): row = [] for j in range(cols1): element = int(input(f"Enter element [{i}][{j}]: ")) row.append(element) matrixA.append(row) # prompt user for elements of second matrix print("Enter elements of matrix 2:") for i in range(rows2): row = [] for j in range(cols2): element = int(input(f"Enter element [{i}][{j}]: ")) row.append(element) matrixB.append(row) # calculate sum of matrices for i in range(rows1): row = [] for j in range(cols1): element = matrixA[i][j] + matrixB[i][j] row.append(element) sum_matrix.append(row) # display matrices and sum print("Matrix A:") for row in matrix1: print(row) print("Matrix B:") for row in matrix2: print(row) print("Sum of matrices:") for row in sum_matrix: print(row)

In this program, the user is prompted to enter the number of rows and columns for each matrix, and then is prompted to enter the elements of each matrix. The program validates that the matrices have the same dimensions before calculating their sum. The matrices are then added together element-wise to produce a third matrix, which is displayed along with the original two matrices.


Here are a few examples of the program output for different input matrices:

Example 1:

Python
Enter number of rows for matrix A: 2 Enter number of columns for matrix A: 3 Enter number of rows for matrix B: 2 Enter number of columns for matrix B: 3 Enter elements of matrix A: Enter element [0][0]: 1 Enter element [0][1]: 2 Enter element [0][2]: 3 Enter element [1][0]: 4 Enter element [1][1]: 5 Enter element [1][2]: 6 Enter elements of matrix B: Enter element [0][0]: 10 Enter element [0][1]: 11 Enter element [0][2]: 12 Enter element [1][0]: 13 Enter element [1][1]: 14 Enter element [1][2]: 15 Matrix A:     [1, 2, 3]     [4, 5, 6] Matrix B:     [10, 11, 12]     [13, 14, 15] Sum of matrices:     [11, 13, 15]     [17, 19, 21]

Example 2:

Python
Enter number of rows for matrix A: 2 Enter number of columns for matrix A: 2 Enter number of rows for matrix B: 2 Enter number of columns for matrix B: 2 Enter elements of matrix A: Enter element [0][0]: 1 Enter element [0][1]: 2 Enter element [1][0]: 3 Enter element [1][1]: 4 Enter elements of matrix B: Enter element [0][0]: 5 Enter element [0][1]: 6 Enter element [1][0]: 7 Enter element [1][1]: 8 Matrix A:     [1, 2]     [3, 4] Matrix B:     [5, 6]     [7, 8] Sum of matrices:     [6, 8]     [10, 12]

Example 3 (matrices with different dimensions):

Python
Enter number of rows for matrix A: 2 Enter number of columns for matrix A: 3 Enter number of rows for matrix B: 3 Enter number of columns for matrix B: 2 Error: matrices cannot be added because they have different dimensions.