简体   繁体   中英

Slicing a matrix with Python

I need to slice 12*12 matrix to 24 2*3 pieces. Matrix of input is:

arr = [
[1,0,1,1,1,0,1,1,1,0,1,0],
[0,0,0,1,0,1,1,1,1,0,1,0],
[0,1,1,0,0,0,1,0,1,0,0,0],
[1,0,0,1,0,0,1,1,1,0,1,1],
[0,0,0,1,0,0,0,1,1,1,1,0],
[0,0,0,0,0,1,1,0,0,0,0,1],
[1,0,1,0,0,0,1,1,0,0,1,1],
[0,0,1,1,0,1,0,1,1,0,1,0],
[0,1,0,0,0,0,1,0,1,0,0,1],
[1,1,0,1,0,1,0,1,0,1,0,0],
[0,0,1,1,1,1,0,1,0,1,1,1],
[0,0,0,0,1,0,0,0,1,1,0,0]]

I try to achieve the task with numpy Matrix:

from sympy import Matrix
Matrix(arr)[:3,:2]

But it will give only one slice from the original matrices.

Matrix([
[1, 0],
[0, 0],
[0, 1]])

What is the convenient way to slice 12*12 matrices to 2*3 pieces? I also need to have dimensions 3*2 of the original, but suppose it's easy after getting the first one ready.

You can use numpy.reshape() function or directly change the shape of the numpy matrix from (12,12) to (24,3,2) , which should give you the result you want.

Example -

In [25]: arr
Out[25]: 
[[1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0],
 [0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0],
 [0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0],
 [1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1],
 [0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0],
 [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1],
 [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1],
 [0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0],
 [0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1],
 [1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0],
 [0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1],
 [0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0]]

In [26]: n = np.array(arr)

In [28]: n.shape
Out[28]: (12, 12)

In [29]: n.shape = (24,3,2)

In [30]: n
Out[30]: 
array([[[1, 0],
        [1, 1],
        [1, 0]],

       [[1, 1],
        [1, 0],
        [1, 0]],

       [[0, 0],
        [0, 1],
        [0, 1]],

       [[1, 1],
        [1, 0],
        [1, 0]],
   .
   .
   .
arr = [
[1,0,1,1,1,0,1,1,1,0,1,0],
[0,0,0,1,0,1,1,1,1,0,1,0],
[0,1,1,0,0,0,1,0,1,0,0,0],
[1,0,0,1,0,0,1,1,1,0,1,1],
[0,0,0,1,0,0,0,1,1,1,1,0],
[0,0,0,0,0,1,1,0,0,0,0,1],
[1,0,1,0,0,0,1,1,0,0,1,1],
[0,0,1,1,0,1,0,1,1,0,1,0],
[0,1,0,0,0,0,1,0,1,0,0,1],
[1,1,0,1,0,1,0,1,0,1,0,0],
[0,0,1,1,1,1,0,1,0,1,1,1],
[0,0,0,0,1,0,0,0,1,1,0,0]]

from sympy import Matrix
row_skip = 3
column_skip = 2

for i in xrange(0, len(arr), row_skip):
    for j in xrange(0, len(arr[0]), column_skip):
        print Matrix(arr)[i:i+row_skip, j:j+column_skip]

Output :

Matrix([[1, 0], [0, 0], [0, 1]])
Matrix([[1, 1], [0, 1], [1, 0]])
Matrix([[1, 0], [0, 1], [0, 0]])
Matrix([[1, 1], [1, 1], [1, 0]])
Matrix([[1, 0], [1, 0], [1, 0]])
Matrix([[1, 0], [1, 0], [0, 0]])
Matrix([[1, 0], [0, 0], [0, 0]])
Matrix([[0, 1], [0, 1], [0, 0]])
Matrix([[0, 0], [0, 0], [0, 1]])
Matrix([[1, 1], [0, 1], [1, 0]])
Matrix([[1, 0], [1, 1], [0, 0]])
Matrix([[1, 1], [1, 0], [0, 1]])
Matrix([[1, 0], [0, 0], [0, 1]])
Matrix([[1, 0], [1, 1], [0, 0]])
Matrix([[0, 0], [0, 1], [0, 0]])
Matrix([[1, 1], [0, 1], [1, 0]])
Matrix([[0, 0], [1, 0], [1, 0]])
Matrix([[1, 1], [1, 0], [0, 1]])
Matrix([[1, 1], [0, 0], [0, 0]])
Matrix([[0, 1], [1, 1], [0, 0]])
Matrix([[0, 1], [1, 1], [1, 0]])
Matrix([[0, 1], [0, 1], [0, 0]])
Matrix([[0, 1], [0, 1], [1, 1]])
Matrix([[0, 0], [1, 1], [0, 0]])

you can change row skip and column skip as you want

This does it. I have a feeling there is a more elegant solution though:

import numpy as np
a=np.array(arr) # also works with a=np.matrix(arr)
[np.split(x,4) for x in np.split(a,6,axis=1)]

Using reshape is better, as @Anand's answer.

in raw python you can use a list expression.

as you would read a book (first right to left than top to bottom):

width = 2
height = 3
[[arr[h+i][w:w+width] for i in range(height)] for h in range(0, len(arr), height) for w in range(0, len(arr), width)]

first top to bottom than right to left:

[[arr[h+i][w:w+width] for i in range(height)] for w in range(0, len(arr), width) for h in range(0, len(arr), height)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM