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.

1 Answer 1

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]]
1
  • Thanks, A[:,0] was something I couldn't figure out. Commented Jun 22, 2013 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.