I would like to know whether there is an equivalent operation of elementwise multiplication in Python as there is to MATLAB. The answer is yes at first, however there is a specific functionality of the elementwise multiplication MATLAB that is every useful, which I cant seem to replicate in python.
In specific if we have matrices A
and b
in MATLAB, and we decide to implement elementwise multiplication, we get the following:
A = [1 2 3] ;
b = [1;
2;
3] ;
C = A.*b
C = [1*1 1*2 1*3 ;
2*1 2*2 2*3 ;
3*1 3*2 3*3] ;
As a python beginner, to perform the same operation in Python my instinct would be to extend the matrices so that they are of identical dimensions and then exploit the basic elementwise multiplication that python offers with numpy.
So is that correct?
For numerical processing, you should use NumPy. Refer to NumPy for Matlab users for help getting started. Here is how you would translate your Matlab code to Python/NumPy.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([1, 2, 3]).reshape(3, 1)
c = a * b
a.shape # (3,)
b.shape # (3, 1)
c
# array([[1, 2, 3],
# [2, 4, 6],
# [3, 6, 9]])
Of course, don't forget to install NumPy.