user3698971
user3698971

Reputation: 307

Matlab Operator .* to python

I have found this .* matlab operation. What is the similar operation in python?

For example how I can convert this matlab code to python.

data.*data_1

Upvotes: 1

Views: 2606

Answers (1)

NPE
NPE

Reputation: 500377

In MATLAB, .* is element-wise product:

octave:1> data = [1 2 3];
octave:2> data_1 = [4 5 6];
octave:3> data .* data_1
ans =
    4   10   18

If you're using Numpy, you could simply write data * data_1:

>>> import numpy as np
>>> data = [1, 2, 3]
>>> data_1 = [4, 5, 6]
>>> np.array(data) * np.array(data_1)
array([ 4, 10, 18])

Without Numpy, you could use a list comprehension:

>>> [i * j for i, j in zip(data, data_1)]
[4, 10, 18]

Upvotes: 6

Related Questions