I have a large dataset containing a 3d array of 16 bit unsigned integer. I want to convert each of the integer into bits and then only retain whose 8:12 bits are "0000" So far I am using a very slow method of loop in three stages:
import numpy as np
# Generate random data
a = np.ones([4,1200,1200], dtype="int16")
# Generate an array which serves later as mask
b = np.zeros(a.shape, dtype=int)
for i in range(4):
for j in range(1200):
for k in range(1200):
b[i,j,k] = int('{:016b}'.format(a[i,j,k])[8:12])
a = np.ma.masked_where(b!=0, a)
I would be thankfut if you could suggest me a clean and fast alternative for this