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

Numpy how to create mask "3D array contains 1D-value"

I have two arrays: 3D-array ndarray(N,M,3) and 1D-array ndarray(3). How to create mask N×M by comparing last axis of 3D array with 1D array?

For now, I can do this with:

mask = (A[:,:,0] == B[0]) & (A[:,:,1] == B[1]) & (A[:,:,2] == B[2])

But there must be a shorter solution.

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 :

mask = (A == B).all(axis=2)

For more information about how this works, search numpy broadcasting.

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