Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Matrix Custom Multiplier

Improve Article
Save Article
  • Last Updated : 05 Feb, 2023
Improve Article
Save Article

Sometimes, while working with data, we can have a problem in which we need to multiply each row of matrix with a different multiplier. This kind of application is important in data science domain. Lets discuss certain ways in which this task can be performed. 

Method #1 : Using loop + zip() The combination of above functions can be used to perform this task. In this, we iterate through each row and perform the task of multiplication using zip(). 

Python3




# Python3 code to demonstrate
# Matrix Custom Multiplier
# using loop + zip()
 
# Initializing list
test_list1 = [[1, 3], [5, 6], [8, 9]]
test_list2 = [4, 3, 6]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Matrix Custom Multiplier
# using loop + zip()
res = []
for mul, sub in zip(test_list2, test_list1):
    temp = []
    for ele in sub:
        temp.append(mul * ele)
    res.append(temp)
 
# printing result
print ("Matrix after custom multiplication : " + str(res))

Output : 

The original list 1 is : [[1, 3], [5, 6], [8, 9]]
The original list 2 is : [4, 3, 6]
Matrix after custom multiplication : [[4, 12], [15, 18], [48, 54]]

  Method #2 : Using list comprehension + zip() The combination of above methods can be used to solve this problem. In this, we just iterate through the list and perform the task of multiplication in one liner. 

Python3




# Python3 code to demonstrate
# Matrix Custom Multiplier
# using list comprehension + zip()
 
# Initializing list
test_list1 = [[1, 3], [5, 6], [8, 9]]
test_list2 = [4, 3, 6]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Matrix Custom Multiplier
# using list comprehension + zip()
res =  [[mul * ele for ele in sub] for mul, sub in zip(test_list2, test_list1)]
 
# printing result
print ("Matrix after custom multiplication : " + str(res))

Output : 

The original list 1 is : [[1, 3], [5, 6], [8, 9]]
The original list 2 is : [4, 3, 6]
Matrix after custom multiplication : [[4, 12], [15, 18], [48, 54]]

Method #3 : Using Numpy

We can use Numpy library to perform this task. Numpy library provides a function “multiply()” which is used to perform the element wise multiplication of two arrays.

Python3




# Python3 code to demonstrate
# Matrix Custom Multiplier
# using Numpy
 
# Importing Numpy library
import numpy as np
 
# Initializing list
test_list1 = [[1, 3], [5, 6], [8, 9]]
test_list2 = [4, 3, 6]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
test_list2 = np.array([4, 3, 6])[:, np.newaxis]
# Matrix Custom Multiplier
# using Numpy
res = np.multiply(test_list1, test_list2)
 
# printing result
print("Matrix after custom multiplication : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

Output :
The original list 1 is : [[1, 3], [5, 6], [8, 9]]
The original list 2 is : [4, 3, 6]
Matrix after custom multiplication : [[ 4 9]
[15 18]
[48 54]]

Time Complexity: O(mn), where m is the number of rows and n is the number of columns.
Auxiliary Space: O(mn)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!