Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm looking at this question: efficient 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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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]]
share|improve this answer
    
Thanks, A[:,0] was something I couldn't figure out. –  Harriv Jun 22 '13 at 16:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.