0% found this document useful (0 votes)
0 views

3Add matrix,transpose

The document provides two Python programs for matrix operations: the first program implements addition, subtraction, multiplication, and transposition of matrices using nested loops, while the second program utilizes the NumPy library for the same operations. The first program includes user input for matrix dimensions and elements, along with a menu for selecting operations. The second program demonstrates matrix operations with predefined matrices using NumPy functions.

Uploaded by

Yash Dhumal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

3Add matrix,transpose

The document provides two Python programs for matrix operations: the first program implements addition, subtraction, multiplication, and transposition of matrices using nested loops, while the second program utilizes the NumPy library for the same operations. The first program includes user input for matrix dimensions and elements, along with a menu for selecting operations. The second program demonstrates matrix operations with predefined matrices using NumPy functions.

Uploaded by

Yash Dhumal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

A-9 Write a Python program to compute following computation on matrix:

a)Addition of two matrices


b) Subtraction of two matrices
c) Multiplication of two matrices
d) Transpose of a matrix

Program 1

def add(m1,m2):

result = [[0,0,0],

[0,0,0],

[0,0,0]]

for i in range(len(m1)):

for j in range(len(m1[0])):

result[i][j] = m1[i][j] + m2[i][j]

for r in result:

print(r)

def mult(m1,m2):

result = [[0,0,0],

[0,0,0],

[0,0,0]]

for i in range(len(m1)):

for j in range(len(m2[0])):

for k in range(len(m2)):

result[i][j] += m1[i][k] * m2[k][j]

for r in result:

print(r)

def sub(m1,m2):

result = [[0,0,0],
[0,0,0],

[0,0,0]]

for i in range(len(m1)):

for j in range(len(m1[0])):

result[i][j] = m1[i][j] - m2[i][j]

for r in result:

print(r)

def tran(m1):

result = [[0,0,0],

[0,0,0],

[0,0,0]]

for i in range (len (m1)):

for j in range (len (m1[0])):

result [j][i] = m1 [i][j]

for r in result:

print (r)

m1 = []

print("Enter 1st matrix m1 :")

r1 = int(input("Enter the number of rows:"))

c1 = int(input("Enter the number of columns:"))

print("Enter the entries rowwise:")

for i in range(r1):

a1 =[]

for j in range(c1):
a1.append(int(input()))

m1.append(a1)

for i in range(r1):

for j in range(c1):

print(m1[i][j], end = " ")

print()

m2 = []

print("Enter 2nd matrix m2 :")

r2 = int(input("Enter the number of rows:"))

c2 = int(input("Enter the number of columns:"))

print("Enter the entries rowwise:")

for i in range(r2):

a2 =[]

for j in range(c2):

a2.append(int(input()))

m2.append(a2)

for i in range(r2):

for j in range(c2):

print(m2[i][j], end = " ")

print()

print("The 1st matrix m1 is :",m1)

print("The 2nd matrix m2 is :",m2)

flag=1

while flag==1:

print("\n\n--------------------MENU--------------------\n")

print("1. Addition of two matrices")


print("2. Subtraction of two matrices")

print("3. Multiplication of two matrices")

print("4. Transpose of matrix")

print("5. Exit\n")

ch=int(input("Enter your Choice (from 1 to 5) :"))

if ch==1:

print("Addition of two matrices is :")

add(m1,m2)

a = input("Do you want to continue (y/n) :")

if a == "y":

flag = 1

else:

flag = 0

print("Thanks for using this program!")

elif ch==2:

print("Subtraction of two matrices is :")

sub(m1,m2)

a = input("Do you want to continue (y/n) :")

if a == "y":

flag = 1

else:

flag = 0

print("Thanks for using this program!")

elif ch==3:

print("Multiplication of two matrices is :")

mult(m1,m2)

a = input("Do you want to continue (y/n) :")


if a == "y":

flag = 1

else:

flag = 0

print("Thanks for using this program!")

elif ch==4:

print("Transpose of matrix m1 is :")

tran(m1)

a = input("Do you want to continue (y/n) :")

if a == "y":

flag = 1

else:

flag = 0

print("Thanks for using this program!")

elif ch==5:

flag=0

print("Thanks for using this program!")

else:

print("!!Wrong Choice!! ")

a=input("Do you want to continue (yes/no) :")

if a=="yes":

flag=1

else:

flag=0

print("Thanks for using this program!")

Program 2 -using numpy library


import numpy

# initializing matrices

x = numpy.array([[1, 2], [4, 5]])

y = numpy.array([[7, 8], [9, 10]])

# using add() to add matrices

print("The element wise addition of matrix is : ")

print(numpy.add(x, y))

# using subtract() to subtract matrices

print("The element wise subtraction of matrix is : ")

print(numpy.subtract(x, y))

# using dot() to multiply matrices

print ("The product of matrices is : ")

print (numpy.dot(x,y))

# using "T" to transpose the matrix

print("The transpose of given matrix is : ")

print(x.T)

print(y.T)

You might also like