w3resource logo
Python Exercises

Python NumPy: Multiply a 5x3 matrix by a 3x2 matrix and create a real matrix product

Python NumPy Math, Statistics, Trigonometry and Linear algebra: Exercise-11 with Solution

Write a Python program to multiply a 5x3 matrix by a 3x2 matrix and create a real matrix product.

Sample Solution:-

Python Code:

import numpy as np
x = np.random.random((5,3))
print("First array:")
print(x)
y = np.random.random((3,2))
print("Second array:")
print(y)
z = np.dot(x, y)
print("Dot product of two arrays:")
print(z)

Sample Output:

First array:                                                           
[[ 0.44349753  0.81043761  0.00771825]                                 
 [ 0.64004088  0.86774612  0.19944667]                                 
 [ 0.61520091  0.24796788  0.93798297]                                 
 [ 0.22156999  0.61318856  0.82348994]                                 
 [ 0.91324026  0.13411297  0.00622696]]                                
Second array:                                                          
[[ 0.73873542  0.06448186]                                             
 [ 0.90974982  0.06409165]                                             
 [ 0.22321268  0.39147412]]                                            
Dot product of two arrays:                                             
[[ 1.06664562  0.08356133]                                             
 [ 1.30677176  0.17496452]                                             
 [ 0.88942914  0.42275803]                                             
 [ 0.90534318  0.37596252]                                             
 [ 0.79804212  0.06992065]]

Python Code Editor:

Improve this sample solution and post your code through Disqus