Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find subarrays that match condition in numpy

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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].

>Solution :

This should do it:

fx[np.in1d(x, sub_x)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading