Examples of Matrices operations in Python Programming

0

In this topic 

We will going to learn about Matrices operations

 

All Contributions

Python program explaining numpy.matlib.identity() function

# importing matrix library from numpy
import numpy as np
import numpy.matlib
# desired 3 x 3 output square identity matrix 
out_mat = np.matlib.identity(3) 
print ("Output matrix : ", out_mat) 

Python program explaining numpy.matlib.eye() function

# importing matrix library from numpy
import numpy as np 
import numpy.matlib
# desired 3 x 3 output matrix 
out_mat = np.matlib.eye(3, k = 0) 
print ("Output matrix : ", out_mat) 

Transpose of a Matrix

We use numpy.transpose to compute transpose of a matrix :

import numpy as np
A = np.array([[1, 1], [2, 1], [3, -3]])
print(A.transpose())

Multiplication of Two Matrices

To multiply two matrices, we use dot() method :

import numpy as np
A = np.array([[3, 6, 7], [5, -3, 0]])
B = np.array([[1, 1], [2, 1], [3, -3]])
C = A.dot(B)
print(C)

 

Addition of Two Matrices

We use + operator to add corresponding elements of two NumPy matrices:

import numpy as np

A = np.array([[2, 4], [5, -6]])
B = np.array([[9, -3], [3, 6]])
C = A + B      # element wise addition
print(C)

total contributions (5)

New to examplegeek.com?

Join us