4

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?

4
  • Could you specify what you mean by multiply A[i] by B[i]? Commented May 7, 2014 at 21:19
  • A[l] is the l-th of those 2d arrays which is a MxN array and l is an integer between 0 and L-1. Likewise B[l] is a 1D array of length M. and I want to multiply transpose of A[l] into B[l]. Commented May 7, 2014 at 21:21
  • Ok, and how is the multiplication of the 1D array with the 2D array defined? Commented May 7, 2014 at 21:23
  • Normall matrix multiplication. I want to multiply transpose of A[l] which would be NxM into B[l] which would be Mx1 and get a Nx1 array Commented May 7, 2014 at 21:26

1 Answer 1

3

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])

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. It is worthwhile taking a look at the docs of np.einsum. This function is extremely versatile.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.