0

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?

xnok
  • 299
  • 2
  • 6
  • 35
  • 3
    `*` is scalar multiplication, you want a matrix-matrix product. Use `numpy.dot` or the `@` operator. – Nico Schlömer Jun 26 '20 at 09:27
  • In MATLAB/Octave `.*` is the element-wise multiplication. `numpy` uses `*`. `np.dot` is the original function for handling matrix multiplication. `@/matmul` is a more recent addition. – hpaulj Jun 26 '20 at 15:50

1 Answers1

1

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.

aleio1
  • 94
  • 1
  • 15