Ilan Ilan - 1 year ago 270
Python Question

python create subset of array from boolean column mask

I need to create a function or equation that will turn this input...

a = [True, False, True]
b = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


into this output...

c = [[1, 3], [4, 6], [7, 9]]


Note: the above arrays are all numpy arrays. Thanks!

Wen Wen
Answer Source

numpy solution

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

array([[1, 3],
       [4, 6],
       [7, 9]])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download