10

I want to have a sub array (between min and max) of a numpy 2D ndarray

    xy_dat = get_xydata()
    x_displayed = xy_dat[((xy_dat > min) & (xy_dat < max))]

min and max are float in order to be compare with the first value of the array xy_dat

xy_dat is a 2D numpy array :

[[ 735964.            1020.        ]
 [ 735964.04166667    1020.        ]
 [ 735964.08333333    1020.        ]
 ..., 
 [ 736613.39722222    1095.        ]
 [ 736613.40416667    1100.        ]
 [ 736613.41111111    1105.        ]]

x_displayed is correctly filtered but I have lost the second value (it is now a 1D array) :

[ 735964.04166667  735964.08333333  735964.125      
 ...,  
736613.39027778  736613.39722222  736613.40416667]

How make the filter on the first value and keep the other ?

reynum
  • 125
  • 1
  • 1
  • 7
  • That's because your comparison is not 2D. For example what does it mean is you have two numbers in one row and one is inside your range and one is not? – piman314 Dec 19 '17 at 11:23

1 Answers1

15

You should perform the condition only over the first column:

x_displayed = xy_dat[((xy_dat[:,0] > min) & (xy_dat[:,0] < max))]

What we do here is constructing a view where we only take into account the first column with xy_dat[:,0]. By now checking if this 1d is between bounds, we construct a 1D boolean array of the rows we should retain, and now we make a selection of these rows by using it as item in the xy_dat[..] parameter.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • `[:,0]` means through all the first array, with only the first element of the second one ? Didn't know, that's neat ! – IMCoins Dec 19 '17 at 11:26
  • @IMCoins: it means the first column of every row yes :) – Willem Van Onsem Dec 19 '17 at 11:27
  • Oh right, it only works with numpy.arrays. I tried it on some python arrays, and some incredible sadness came into my eyes. :'( – IMCoins Dec 19 '17 at 11:34
  • @IMCoins You can always subclass lists and overload `__getitem__` yourself :) – cs95 Dec 19 '17 at 11:44
  • @IMCoins: well we can construct a subclass of the Python vanilla lists to let it work with lists as well. A possible problem however is that in Python items can be hybrid and the sublists can have different size. So how would you work with `[1, [], [14], 'ab']`? Simply ignore non-sublists and empty lists? – Willem Van Onsem Dec 19 '17 at 11:48