1

I need a faster/optimised version of my current code:

import numpy as np

a = np.array((1, 2, 3))
b = np.array((10, 20, 30, 40, 50, 60, 70, 80))

print([i*b for i in a])

Is there any faster way to do this using numpy functions (maybe without reshaping and blowing up the whole thing)?

1 Answer 1

11

Looks like the outer product.

>>> np.outer(a, b)
array([[ 10,  20,  30,  40,  50,  60,  70,  80],
       [ 20,  40,  60,  80, 100, 120, 140, 160],
       [ 30,  60,  90, 120, 150, 180, 210, 240]])
Sign up to request clarification or add additional context in comments.

1 Comment

This is it, exactly ; )

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.