Skip to content
Advertisement

How to multiply specific rows/columns of matrices with each other in python?

I have to input matrices of shape

m1: (n,3)
m2: (n,3)

I want to multiply each row (each n of size 3) with its correspondence of the other matrix, such that i get a (3,3) matrix for each row.

When im trying to just use e.g. m1[0]@m2.T[0] the operation doesnt work, as m[0] delivers a (3,) list instead of a (3,1) matrix, on which i could use matrix operations.

Is there a relatively easy or elegant way to get the desired (3,1) matrix for the matrix multiplication?

Advertisement

Answer

By default, numpy gets rid of the singleton dimension, as you have noticed.
You can use np.newaxis (or equivalently None. That is an implementation detail, but also works in pytorch) for the second axis to tell numpy to “invent” a new one.

import numpy as np
a = np.ones((3,3))
a[1].shape                 # this is (3,)
a[1,:].shape               # this is (3,)
a[1][...,np.newaxis].shape # this is (3,1)

However, you can also use dot or outer directly:

>>> a = np.eye(3)
>>> np.outer(a[1], a[1])
array([[0., 0., 0.],
       [0., 1., 0.],
       [0., 0., 0.]])
>>> np.dot(a[1], a[1])
1.0
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement