Filter a 2D numpy array

前端 未结 1 1257
闹比i
闹比i 2020-12-19 14:39

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 &l         


        
相关标签:
1条回答
  • 2020-12-19 15:26

    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.

    0 讨论(0)
提交回复
热议问题