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 retrieve elements from an array without slicing?

Say I have an array, A1. B1 is another array storing the last two rows of A1. I need to retrieve the first two rows of A1 without slicing as another array (C1). Logically, I’m thinking something like A1 (the whole array) – B1 (the last two rows) = C1 (the first two rows), but I don’t know if Python does anything like that. How do I get elements from an array without slicing? Appreciate your help!

A1 = np.array([ [1, 4, 6, 8],[2, 5, 7, 10],[3, 6, 9, 13],[11, 12, 16, 0]])
B1 = A1[2:4,]

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 :

Instead of ‘subtracting’ the two arrays you could delete the last two rows:

C1 = np.delete(A1, (2,3), axis=0)
array([[ 1,  4,  6,  8],
       [ 2,  5,  7, 10]])
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