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

How to save the interleaved values of an array?

I don’t know how to put it in words, but I can with an example. I have the following variable

anglesp = np.linspace(0, 2*np.pi,50)

but I want to extract to a new variable the values at the position 0,2,4,…,50 and so on. Something like this:

angles = anglesp[0,2,4,6...]

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

>Solution :

numpy arrays can take an iterable (of integers) as an index instead of a single position index. In this case, the [] operation will return a new array containing the items at the specified positions within the iterable.

As an example:

>>> A = np.array([0,10,20,30,40])
>>> idx = [0, 2, 4]
>>> A[idx]
np.array([0, 20, 40])

So, in your case, you simply need:

angles = anglesp[range(0,50+1,2)]
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