soroosh.strife
soroosh.strife

Reputation: 1191

multiply multidimensional arrays in python

I have stored a number of 2d arrays in a 3d array and I need to multiply each one with a vector. so I have stored all those vectors in a 2d array. It's like this:

A = np.random.random((L, M, N))
B = np.random.random((L, M))

and I need to multiply each A[l] by B[l] which results in a Nx1 array and the output of the whole operation would be a LxN 2d array. Is there a function that can do this or do I need a loop?

Upvotes: 4

Views: 2439

Answers (1)

eickenberg
eickenberg

Reputation: 14377

An option is np.einsum

import numpy as np
output = np.einsum("ijk, ij -> ik", A, B)

This results in a (L, N) sized array containing matrix products of all the A[i].T.dot(B[i])

Upvotes: 3

Related Questions