How to create a sub array from an array in Python?

1

I was able to generate an array via Python and through this generated array I had to generate a 3x3 array. But I do not know how to do this, I was researching and I know that Python itself has a resource for this, but I do not think so.

Example:

I have the Matrix:

Result

AndthroughthisgeneratedMatrixitwouldreturnmeother3x3Matrix,asintheonebelow:

If anyone can help me and I will be extremely grateful!

Thank you!

    
asked by anonymous 11.02.2017 / 02:10

1 answer

3

Simple: use the NumPy package and slicing . Example:

import numpy as np

matriz = np.array([
            [13, 28, 45, 50, 26, 10],
            [27, 24, 22, 33, 88, 11],
            [90, 25, 85, 23, 76, 55],
            [77, 15, 31, 29, 13, 14],
            [66, 41, 50, 20, 47, 11]
         ])

print(matriz[0:3, 1:4])
print('')
print(matriz[2:, 0:3])

Output:

[[28 45 50]
 [24 22 33]
 [25 85 23]]

[[90 25 85]
 [77 15 31]
 [66 41 50]]

See working at Ideone .

    
11.02.2017 / 02:31