0

Suppose I have the following data:

mask = [[0, 1, 1, 0, 1]] # 2D mask
ip_array = [[7, 4, 5, 2, 3]
            [3, 2, 1, 9, 0]
            [1, 8, 6, 3, 1]] # 2D array

I want to multiply the mask with each row of ip_array. So the output should be like:

[[0, 4, 5, 0, 3]
 [0, 2, 1, 0, 0]
 [0, 8, 6, 0, 1]]

I am new to numpy functions and I am looking for an efficient way to do this. Any help is appreciated!

Animeartist
  • 1,047
  • 1
  • 10
  • 21

1 Answers1

0

You can use:

np.multiply(mask, ip_array)

Giving you:

array([[0, 4, 5, 0, 3],
       [0, 2, 1, 0, 0],
       [0, 8, 6, 0, 1]])

Also, as a heads-up, you're missing two commas in your definition of ip_array. It should look like this:

ip_array = [[7, 4, 5, 2, 3],
            [3, 2, 1, 9, 0],
            [1, 8, 6, 3, 1]] # 2D array
vtasca
  • 1,660
  • 11
  • 17