Search code examples
pythonnumpyvectortransposescatter-matrix

Calculating a vectors with its transposed vector


I'm working on a calculation for a within matrix scatter where i have a 50x20 vector and something that occured to me is that multiplying transposed vectors by the original vector, gives me a dimensional error, saying the following:

operands could not be broadcast together with shapes (50,20) (20,50)

What i tried is: array = my_array * my_array_transposed and got the aforementioned error.

The alternative was to do, then:

new_array = np.dot(my_array,  np.transpose(my_array))

In Octave for instance, this would've been a lot easier, but due to the size of the vector, it's kinda hard for me to confirm for ground truth if this is the way to do the following calculation:

enter image description here

Because as far as i know, there is something related as to whether the multiplication is element wise.

My question is, am i applying that formula the right way? If not, whats the right way to multiply a transposed vector by the non-tranposed vector?


Solution

  • Yes, the np.dot formula is the correct one. If you write array = my_array * my_array_transposed you are asking Python to perform component-wise multiplication. Instead you need a row-by-column multiplication which is achieved in numpy with np.dot.