Free cookie consent management tool by TermsFeed The matmul() function of numpy | Pythontic.com

The matmul() function of numpy

Overview:

  • The matmul() function of NumPy module multiplies two n-dimensional matrices and returns the product of the two matrices. 

Example - Muliplication of two 2-dimensional matrices using matmul() of numpy function:

# Example Python program that multiplies two-dimensional matrices using
# the numpy functoin matmul()
import numpy
import random

# Create ndarrays of numbers
n1 = numpy.ndarray(shape = (3, 3), 
                   dtype = numpy.int32)

n2 = numpy.ndarray(shape = (3, 3), 
                   dtype = numpy.int32)

# Populate the numbers
for i in range (0, 3):
    for j in range (0, 3):
        n1[i][j] = random.randrange(2, 20)
        n2[i][j] = random.randrange(2, 10)

# Matrix multiplication of two-dimensional numpy
# arrays
result = numpy.matmul(n1, n2)
print(result)

Output:

[[210 128 138]

 [244 173 237]

 [314 208 258]]

 


Copyright 2025 © pythontic.com