Lyrk
Lyrk

Reputation: 2000

Passing array to an array in numpy

import numpy as np

arr1= np.array([10,20,30,40,50])
arr2= np.array([False, False,  True,  True,  True])

result=arr1[arr2] #[30 40 50]

Having hard time to grasp this. When another numpy array is passed to an existing numpy array, all elements multiplied?

Upvotes: 0

Views: 232

Answers (1)

sacuL
sacuL

Reputation: 51395

You're indexing a numpy array by another array, rather than passing an array to another array. In your case, since you're using a boolean index, it's creating a mask, and only returning the values of arr1 where arr2 is True.

Upvotes: 3

Related Questions