NumPy Matrix and Vector Multiplication

In this tutorial, we will learn how to find the multiplication of NumPy matrix and vector. By Pranit Sharma Last updated : May 24, 2023

Problem statement

Given a NumPy matrix and vector, we have to find the NumPy matrix and vector multiplication.

Calculating NumPy Matrix and Vector Multiplication

For NumPy matrix and vector multiplication, you can simply use numpy.dot() method, it will perform the multiplication or dot operation on matrix and vector. Let suppose there are two NumPy arrays a1 and a2, the by using the a1.dot(a2) will return the result like matrix multiplication.

Let us understand with the help of an example,

Python Program for NumPy Matrix and Vector Multiplication

# Import numpy
import numpy as np

# Creating numpy arrays
a1 = np.array([
    [ 5, 1 ,3],
    [ 1, 1 ,1],
    [ 1, 2 ,1]])

a2 = np.array([1, 2, 3])

# Display original arrays
print("Original array 1:\n",a1,"\n")
print("Original array 2:\n",a2,"\n")

# Multiplying like matrix
res = a1.dot(a2)

# Display result
print("Result:\n",res)

Output

The output of the above program is:

Original array 1:
 [[5 1 3]
 [1 1 1]
 [1 2 1]] 

Original array 2:
 [1 2 3] 

Result:
 [16  6  8]

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.