1

So I have 2 array. Actually my array size is more than this, it's 700 data. but as an example on a small scale as below

A = np.array([1.1, 1.2, 2.1, 2.2])
B = np.array([1, 3, 4, 6, 7, 8, 9, 10])

I want to do something with mathematical modeling like the following

C = [[(1.1*1)+(1.1*3)+(1.1*4)+(1.1*6)+(1.1*7)+(1.1*8)+(1.1*9)+(1.1*10)],
    [(1.2*1)+(1.2*3)+(1.2*4)+(1.2*6)+(1.2*7)+(1.2*8)+(1.2*9)+(1.2*10)],
    [(2.1*1)+(2.1*3)+(2.1*4)+(2.1*6)+(2.1*7)+(2.1*8)+(2.1*9)+(2.1*10)],
    [(2.2*1)+(2.2*3)+(2.2*4)+(2.2*6)+(2.2*7)+(2.2*8)+(2.2*9)+(2.2*10)]]

Where this result is displayed in an array with 4 rows and 1 column.

How do I write this logic in python

Please help, thank you

1 Answers1

4

My solution is inspired by this answer.

Try this:

np.sum(A[:, None] * B[None, :], axis=1)

A[:, None] * B[None, :] generates the product of the terms in the Cartesian product. Then you sum along the correct axis so that the terms you are summing keep the value from A fixed and shift along the values in B.

Also, the term for C you provide is missing the terms that multiply by 8. I can't edit the question, so I'll provide the correct term here:

C = [[(1.1*1)+(1.1*3)+(1.1*4)+(1.1*6)+(1.1*7)+(1.1*8)+(1.1*9)+(1.1*10)],
    [(1.2*1)+(1.2*3)+(1.2*4)+(1.2*6)+(1.2*7)+(1.2*8)+(1.2*9)+(1.2*10)],
    [(2.1*1)+(2.1*3)+(2.1*4)+(2.1*6)+(2.1*7)+(2.1*8)+(2.1*9)+(2.1*10)],
    [(2.2*1)+(2.2*3)+(2.2*4)+(2.2*6)+(2.2*7)+(2.2*8)+(2.2*9)+(2.2*10)]]