How to get desired output from 5 x 5 matrix | Numpy |

Issue

I am solving a below question where i want to print the expected output from 5 x 5 matrix

5 x 5 matrix

0,  1,  3,   3,  4 
11,  6, 54,  8,  9 
12, 11, 10, 18,  4 
17, 16, 19, 10,  1 
21, 21, 52, 33, 22 

Expected output :

12 10
21 52

I tried this not able to apply logic

arr[2:,:3:2]

Solution

Seems like you got some assignment work

Below code might work for you just try it

import numpy as np

arr=np.array([
[0,  1,  3,   3,  4 ],
[11,  6, 54,  8,  9 ],
[12, 11, 10, 18,  4 ],
[17, 16, 19, 10,  1 ],
[21, 21, 52, 33, 22 ] ] )

arr[2:,:3:2][::2]

Expected output :

array([[12, 10],
       [21, 52]])

Note : The code is tested on Google Collab

Incase found any issue feel free to edit it

Answered By – codeholic24

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published