You are on page 1of 5

practical no.

4
A)
AIM:
Write a python program to perform the following operation on two matrices
1. Addition
2. Subtraction
3. Multiplication
4. Division

5. Transpose

Code:

import numpy

x=numpy.array([[1,2],

[4,5]])

y=numpy.array([[7,8],

[9,10]])

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

print(numpy.add(x,y))

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

print(numpy.subtract(x,y))
print("The element wise multiplication of matrix is:")

print(numpy.multiply(x,y))

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

print(numpy.divide(x,y))

print("Transpose of matrix.")

print(x.T)

Output:

B)
AIM:
Write a python program to perform the following
1. Accept the columns and rows value for a matrix from the user
2. Scalar multiplication
3. Transpose of the matrix

Code:

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

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

matrix =[]

for i in range (R):

a=[]

for j in range(C):

a.append(int(input()))

matrix.append(a)

for i in range(R):

for j in range(C):

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

print()

print("Rows:")

for i in range (len(matrix)):

print (matrix[i])
print("cols:")

col=[]

for j in range(C):

b=[]

for i in range(R):

b.append(matrix[i][j])

col.append(b)

print(col)

scalar=int(input("Enter a scalar to be multiplied:"))

sca=[]

for i in range(R):

m=[]

for j in range (C):

c=scalar*matrix[i][j]

m.append(c)

sca.append(m)
print(sca)

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

for i in range (C):

for j in range (R):

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

print()

Output:

You might also like