Home > other >  Python function for multiplication of arrays by index
Python function for multiplication of arrays by index

Time:11-12

I want to write a function that takes two arguments, (1) an n x m matrix and (2) a 1 x n matrix, and that multiplies each row by the corresponding index of the scaling matrix.

Can someone help me with this?

CodePudding user response:

Have you checked numpy library? it makes working with matrices a lot easier. You can check it out in this links: https://www.tutorialspoint.com/python_data_structure/python_matrix.htm

numpy gives you a .dot method for matrix multiplication. Hope this guides you enough --> https://numpy.org/doc/stable/reference/generated/numpy.dot.html#numpy.dot

CodePudding user response:

If you have a matrix like

mat=np.array([[1,2,3,4,5],
[2,3,4,5,6],
[3,4,5,6,7]])

and an array like

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

Then the required function using numpy multiplication can be so simple:

def multiplicate(mat,arr):
    mat*arr.reshape((3,1))
  • Related