- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Matrix product of a 2D (first argument) and a 1D array (second argument) in Numpy

To find the matrix product of a 2D and a 1D array, use the **numpy.matmul()** method in Python Numpy. If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed.

Returns the matrix product of the inputs. This is a scalar only when both x1, x2 are 1-d vectors. The out is a location into which the result is stored. If provided, it must have a shape that matches the signature (n,k),(k,m)->(n,m). If not provided or None, a freshly-allocated array is returned.

## Steps

At first, import the required library −

import numpy as np

Create a 2D and a 1D array −

arr1 = np.array([[5, 7], [10, 15]]) arr2 = np.array([25, 35])

Display the arrays −

print("Array 1 (Two Dimensional)...

", arr1) print("

Array 2 (One Dimensional)...

", arr2)

Get the type of the arrays −

print("

Our Array 1 type...

", arr1.dtype) print("

Our Array 2 type...

", arr2.dtype)

Get the dimensions of the Arrays −

print("

Our Array 1 Dimensions...

",arr1.ndim) print("

Our Array 2 Dimensions...

",arr2.ndim)

Get the shape of the Arrays −

print("

Our Array 1 Shape...

",arr1.shape) print("

Our Array 2 Shape...

",arr2.shape)

To find the matrix product of a 2D and a 1D array, use the numpy.matmul() method in Python Numpy. If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. After matrix multiplication the appended 1 is removed −

print("

Result (matrix product)...

",np.matmul(arr1, arr2))

## Example

import numpy as np # Create a 2D and a 1D array arr1 = np.array([[5, 7], [10, 15]]) arr2 = np.array([25, 35]) # Display the arrays print("Array 1 (Two Dimensional)...

", arr1) print("

Array 2 (One Dimensional)...

", arr2) # Get the type of the arrays print("

Our Array 1 type...

", arr1.dtype) print("

Our Array 2 type...

", arr2.dtype) # Get the dimensions of the Arrays print("

Our Array 1 Dimensions...

",arr1.ndim) print("

Our Array 2 Dimensions...

",arr2.ndim) # Get the shape of the Arrays print("

Our Array 1 Shape...

",arr1.shape) print("

Our Array 2 Shape...

",arr2.shape) # To find the matrix product of a 2D and a 1D array, use the numpy.matmul() method in Python Numpy # If the second argument is 1-D, it is promoted to a matrix by appending a 1 to its dimensions. # After matrix multiplication the appended 1 is removed. print("

Result (matrix product)...

",np.matmul(arr1, arr2))

## Output

Array 1 (Two Dimensional)... [[ 5 7] [10 15]] Array 2 (One Dimensional)... [25 35] Our Array 1 type... int64 Our Array 2 type... int64 Our Array 1 Dimensions... 2 Our Array 2 Dimensions... 1 Our Array 1 Shape... (2, 2) Our Array 2 Shape... (2,) Result (matrix product)... [370 775]

- Related Articles
- Matrix product of a 1D (first argument) and a 2D array (second argument) in Numpy
- Compute the bit-wise AND of a 1D and a 2D array element-wise in Numpy
- Compute the bit-wise XOR of a 1D and a 2D array element-wise in Numpy
- Compute the bit-wise OR of a 1D and a 2D array element-wise in Numpy
- Determine if the type in the first argument is a subclass of second in Python
- Evaluate a 2D Legendre series on the Cartesian product of x and y with 1d array of coefficient in Python
- How to convert a 2D array into 1D array in C#?
- Product of maximum in first array and minimum in second in C
- Return the reciprocal of the argument element-wise in Numpy
- Scatter a 2D numpy array in matplotlib
- Difference Between One-Dimensional (1D) and Two-Dimensional (2D) Array
- Print a 2D Array or Matrix in Java
- Return the default fill value for the argument object in Numpy
- Matrix product of two arrays in Numpy
- Argument Index in Java