Skip to content Skip to sidebar Skip to footer

Filter A 2d Numpy Array

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

Solution 1:

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.

Post a Comment for "Filter A 2d Numpy Array"