0

I have two array a and b such that b's shape is a row of a. I want to multiply b to each row of a so for example,

#a

[[1,2,3]
 [4,5,6]
 [7,8,9]]

#b
[1,3,2]

# a times b
[[1,6,6]
 [4,15,12]
 [7,24,18]]

Is there any way to perform it?

2
  • 1
    As simple as a * b Commented Nov 16, 2020 at 0:26
  • See broadcasting as well, multiplication will yield expecting results only if the dimensions make sense. numpy.org/doc/stable/user/basics.broadcasting.html Commented Nov 16, 2020 at 10:35

1 Answer 1

2

This is actually the expected output when using numpy. So in your case:

import numpy as np
a = np.array([[1,2,3], [4,5,6], [7,8,9]])
b = np.array([1,3,2])
c = a*b

c will be:

array([[ 1,  6,  6],
       [ 4, 15, 12],
       [ 7, 24, 18]])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.