Reputation: 31
I have an array like the following
Table = [[True, False, False, False, False],
[False, False, True, False, False],
[True, False, False, True, False]]
Is there any way to mask all column by column to MaskTable, the final result as
MaskTable = [True, False, True, True, False]
Upvotes: 1
Views: 84
Reputation: 42143
If you are not using numpy, you can filter the columns using zip() within a list comprehension:
Filtered = [ [v for v,m in zip(row, MaskTable) if m] for row in Table ]
If you do use numpy, your table and mask must be numpy objects:
import numpy as np
Table = np.array([[True, False, False, False, False],
[False, False, True, False, False],
[True, False, False, True, False]])
MaskTable = np.array([True, False, True, True, False])
Filtered = Table[:,MaskTable]
The above is the answer to your question as formulated in the title.
Your sample output, however, suggest that what you are actually trying to do is to apply an OR relation column-wise in your matrix:
MaskTable = np.any(Table,axis=0)
or
MakTable = np.logical_or.reduce(Table,axis=0)
Upvotes: 0
Reputation: 31
Try:
MaskTable = np.any(Table, axis = 0)
Output:
array([ True, False, True, True, False])
This uses numpy.any(), which checks if any of the values along the given axis evaluate to True. numpy is imported as np here.
Upvotes: 2