SE Computer Engineering Practical 8 solution

Home » All Blogs » Python » Python assignments » SE Computer Engineering » SE Computer Engineering Practical 8 solution

SE Computer Engineering Practical 8 solution

Question:

Write a Python program to compute the following computation on matrix:
a) Addition of two matrices
b) Subtraction of two matrices
c) Multiplication of two matrices
d) Transpose of a matrix

Code:

import numpy

# initializing matrices
x = numpy.array([[1, 2], [4, 5]])
y = numpy.array([[7, 8], [9, 10]])

print('Matrices are:', x, y)

# 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)

Output:

Matrices are: [[1 2]
 [4 5]] [[ 7  8]
 [ 9 10]]
The element wise addition of matrix is : 
[[ 8 10]
 [13 15]]
The element wise subtraction of matrix is : 
[[-6 -6]
 [-5 -5]]
The product of matrices is : 
[[25 28]
 [73 82]]
The transpose of given matrix is : 
[[1 4]
 [2 5]]

Process finished with exit code 0
Tech Amplifier Final Logo