oleron
oleron

Reputation: 483

Get the product of two one dimensional numpy matrices

I have two one-dimensional numpy matrices:

[[ 0.69 0.41]] and [[ 0.81818182 0.18181818]]

I want to multiply these two to get the result

[[0.883, 0.117]] (the result is normalized)

If I use np.dot I get ValueError: matrices are not aligned

Does anybody have an idea what I am doing wrong?

EDIT

I solved it in a kind of hacky way, but it worked for me, regardless of if there is a better solution or not.

new_matrix = np.matrix([ a[0,0] * b[0,0], a[0,1] * b[0,1] ])

Upvotes: 1

Views: 1145

Answers (1)

askewchan
askewchan

Reputation: 46530

It seems you want to do element-wise math. Numpy arrays do this by default.

In [1]: import numpy as np

In [2]: a = np.matrix([.69,.41])

In [3]: b = np.matrix([ 0.81818182, 0.18181818])

In [4]: np.asarray(a) * np.asarray(b)
Out[4]: array([[ 0.56454546,  0.07454545]])

In [5]: np.matrix(_)
Out[5]: matrix([[ 0.56454546,  0.07454545]])

Upvotes: 1

Related Questions