Home > OS >  Find subarrays that match condition in numpy
Find subarrays that match condition in numpy

Time:03-31

Suppsoe I have an array of size 10:

x = np.linspace(0.1, 1, 10)

I have an array of function values at these x's:

fx = np.sin(x)

Now I have a subarray of x, say, for example,

sub_x = x[::2]

How do I extract the values of fx at the sub-points in sub_x? I.e.,

fx[indices of x that are in sub_x]

This does not work: fx[x == sub_x].

CodePudding user response:

This should do it:

fx[np.in1d(x, sub_x)]

CodePudding user response:

You can do it with np.isin as:

fx[np.isin(x, sub_x)]
  • Related