Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mask python array based on multiple column indices

I have a 64*64 array, and would like to mask certain columns. For one column I know I can do:

mask = np.tri(64,k=0,dtype=bool)

col = np.zeros((64,64),bool)
col[:,3] = True
col_mask = col + np.transpose(col)
col_mask = np.tril(col_mask)
col_mask = col_mask[mask]

but how to extend this to multiple indices? I have tried doing col[:,1] & col[:,2] = True but got
SyntaxError: cannot assign to operator

Also I might have up to 10 columns I would like to mask, so is there a less unwieldily approach? I have also looked at numpy.indices but I don’t think this is what I need. Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can update multiple indices at the same time:

idx = [1,2,3,10,50]
col[:,idx] = True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading