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 get subarray of matrix in python

say I have the following array j:

[[1, 2, 3, 4, 5], 
 [7, 7, 7, 6, 4], 
 [1, 1, 2, 0, 0]]

how can I get the subarray of 2×2 so the subarray would be:

[[1, 2], 
 [7, 7],]

intuitively I assumed j[0:2][0:2] would do the trick but I get:

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

[[1, 2, 3, 4, 5], [7, 7, 7, 6, 4]]

>Solution :

You need to explicitly say what you want from each row:

[r[0:2] for r in j[0: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