0

I'm looking at this question: Efficient thresholding filter of an array with numpy

I have a similar problem, but with a two dimensional array, where several coordinates and values are stored in rows of a NumPy array. How I can do a similar filtering like in the question above?

My data looks like this:

>>> A.shape
(4269862, 5)

>>> A[0]
array([  2.27011719e+02,   0.00000000e+00,   2.88134766e+02,
         2.00000000e+00,   7.69880000e+04], dtype=float32)

And these values correspond to X, Y, Z and value1 and value2. What I want is to efficiently get eg. all rows with X in 300--400, Y in 200--250 and Z in 200--300.

Community
  • 1
  • 1
Harriv
  • 6,029
  • 6
  • 44
  • 76

1 Answers1

4

You can create a boolean mask that will be true when all your conditions are met:

idx = ((A[:, 0] > 300) & (A[:, 0] < 400) & 
       (A[:, 1] > 200) & (A[:, 1] < 250) & 
       (A[:, 2] > 200) & (A[:, 2] < 300))

print A[idx]   
# this should give your array rows where idx is True

You can test this:

A = np.random.uniform(150, 500, (200, 5)).astype('i')
idx = ((A[:, 0] > 300) & (A[:, 0] < 400) & 
       (A[:, 1] > 200) & (A[:, 1] < 250) & 
       (A[:, 2] > 200) & (A[:, 2] < 300))
print A[idx]
#[[339 292 231 211 474]
# [371 252 310 281 256]
# [337 263 471 159 397]
# [361 299 383 250 206]
# [360 278 328 194 453]
# [360 258 205 245 427]
# [339 286 331 175 418]]
tiago
  • 22,602
  • 12
  • 72
  • 88