1

I´m trying to multiply each row of array A by each of all the rows in another array (B) in order to get len(A) number of arrays with same number of rows and columns as the first two arrays.

Any help?

pseudo-code
from numpy  import *
import numpy as np

def multipar():
    A = array( [ (0.1,0.5,0.2,0.2), (0.2,0.5,0.1,0.2), (0.7,0.1,0.1,0.1) ] )
    B = array( [ (1,2,3,4), (2,3,4,5), (3,4,5,6) ] )
    for i in len(A):
        average = A[i]*B
    print average

multipar() 

I would like to have each resulting new array

Array C
(0.1,0.5,0.2,0.2) * (1,2,3,4);
(0.1,0.5,0.2,0.2) * (2,3,4,5);
(...)
Array D
(0.2,0.5,0.1,0.2) * (1,2,3,4);
(...)

1 Answer 1

2

You could do something interesting with higher dimensions. Extend either A or B into the third dimension, then multiply that with the one that wasn't extended. e.g.:

A = array( [ (0.1,0.5,0.2,0.2), (0.2,0.5,0.1,0.2), (0.7,0.1,0.1,0.1) ] )
B = array( [ (1,2,3,4), (2,3,4,5), (3,4,5,6) ] )

tiled = tile (B, (3,1,1)).swapaxes (0,1)
all_results = A*tiled

Now you have all of your result arrays in all_results; you can easily get them with all_results[0], all_results[1], etc

EDIT: In response to the latest question edit: If you really need the result arrays separately, then there are two further options:

  • C, D, E = all_results
  • replace the last two statements in my first suggestion with:

    C = B * A[0]

    D = B * A[1]

    E = B * A[2]

If you really need separate arrays for the results, and with many more rows so that a loop becomes necessary, then you can do something like (thanks @Jaime for the broadcasting notation)

all_results = A[:, None, :] * B[None, :, :]
for i, res in enumerate (all_results):
    locals () ['result%d'%i] = res

Now the result of multiplying by the first row is in the variable called res1, the second row in res2, and so forth.

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

3 Comments

thanks for your help. It would be better to have the result arrays separately. Since my real data has many more rows than this example, how would you implement your last option with a loop? How would it difer from my pscode? Best regards
@lxop You don't need to tile, use broadcasting instead! If A.shape = (r, t) and B.shape = (s, t), then C = A[:, None, :] * B[None, :, :] will have shape (r, s, t) and C[0] will be the product of the first row of A with B. A[:, None, :] is equivalent to A.reshape(r, 1, t) and B[None, :, :] is the same as B.reshape(1, s, t). Broadcasting reuses data, while tiling copies it, so the former is both faster and uses less memory than the latter.
@Pedro9 If you have many more rows than this example, surely keeping everything in one array and accessing it with indices would be easier? But in any case I'll edit the answer to give an example for how you could get individual result arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.