Table of Contents

Python code for Addition and subtraction of two matrices using lists.

 

Python code implementation without user-defined functions & classes

Code: 
#Python code for Addition and subtraction of two matrices using list

a = [[1, 2, 3], [4, 5, 6]]
b = [[7, 8, 9], [10, 11, 12]]

def add_sub_matrix_func(matrix_a, matrix_b):
    matrix_a_row_size = len(matrix_a)
    matrix_a_column_size = len(matrix_a[0])
    matrix_b_row_size = len(matrix_b)
    matrix_b_column_size = len(matrix_b[0])
    c_tmp = matrix_a
    d_tmp = matrix_b
    if (matrix_a_row_size == matrix_b_row_size and matrix_a_column_size == matrix_b_column_size):
        print("Addition and Subtraction of Two given Matrix is Possible")
    else:
        print("Size of Two matrix differs: Operation not Possible")
        exit()

    for i in range(0, matrix_a_row_size):
        for j in range(0, matrix_a_column_size):
            c_tmp[i][j] = matrix_a[i][j] + matrix_b[i][j]
            d_tmp[i][j] = matrix_a[i][j] - matrix_b[i][j]

    print("Addition result: ", c_tmp)
    print("Subtraction result: ", d_tmp)

add_sub_matrix_func(a,b)

 

Output: 
Addition and Subtraction of Two given Matrix is Possible
Addition result:  [[8, 10, 12], [14, 16, 18]]
Subtraction result:  [[1, 2, 3], [4, 5, 6]]

 

 

Python code implementation using function

Code: 
#Python code for Addition and subtraction of two matrices using list

a = [[1, 2, 3], [4, 5, 6]]
b = [[7, 8, 9], [10, 11, 12]]

def add_sub_matrix_func(matrix_a, matrix_b):
    matrix_a_row_size = len(matrix_a)
    matrix_a_column_size = len(matrix_a[0])
    matrix_b_row_size = len(matrix_b)
    matrix_b_column_size = len(matrix_b[0])
    c_tmp = matrix_a
    d_tmp = matrix_b
    if (matrix_a_row_size == matrix_b_row_size and matrix_a_column_size == matrix_b_column_size):
        print("Addition and Subtraction of Two given Matrix is Possible")
    else:
        print("Size of Two matrix differs: Operation not Possible")
        exit()

    for i in range(0, matrix_a_row_size):
        for j in range(0, matrix_a_column_size):
            c_tmp[i][j] = a[i][j] + b[i][j]
            d_tmp[i][j] = a[i][j] - b[i][j]

    print("Addition result: ", c_tmp)
    print("Subtraction result: ", d_tmp)

add_sub_matrix_func(a,b)

 

Output: 
Addition and Subtraction of Two given Matrix is Possible
Addition result:  [[8, 10, 12], [14, 16, 18]]
Subtraction result:  [[1, 2, 3], [4, 5, 6]]

 

 

Python code implementation using Classes

Code: 
#Python code for Addition and subtraction of two matrices using class

a = [[1, 2, 3], [4, 5, 6]]
b = [[7, 8, 9], [10, 11, 12]]

class Add_sub_matrix_class(object):
    def add_sub_matrix_func(self, matrix_a, matrix_b):
        self.matrix_a = matrix_a
        self.matrix_b = matrix_b
        matrix_a_row_size = len(self.matrix_a)
        matrix_a_column_size = len(self.matrix_a[0])
        matrix_b_row_size = len(self.matrix_b)
        matrix_b_column_size = len(self.matrix_b[0])
        c_tmp = self.matrix_a
        d_tmp = self.matrix_b
        if (matrix_a_row_size == matrix_b_row_size and matrix_a_column_size == matrix_b_column_size):
            print("Addition and Subtraction of Two given Matrix is Possible")
        else:
            print("Size of Two matrix differs: Operation not Possible")
            exit()

        for i in range(0, matrix_a_row_size):
            for j in range(0, matrix_a_column_size):
                c_tmp[i][j] = self.matrix_a[i][j] + self.matrix_b[i][j]
                d_tmp[i][j] = self.matrix_a[i][j] - self.matrix_b[i][j]

        print("Addition result: ", c_tmp)
        print("Subtraction result: ", d_tmp)


#Create Object
Object_1 = Add_sub_matrix_class()

#Call function using created object
Object_1.add_sub_matrix_func(a,b)

 

Output: 
Addition and Subtraction of Two given Matrix is Possible
Addition result:  [[8, 10, 12], [14, 16, 18]]
Subtraction result:  [[1, 2, 3], [4, 5, 6]]

 

 

 

 

Enjoy Python Code By Pythonbaba 🙂