1

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.

MarkokraM
  • 980
  • 1
  • 12
  • 26
  • Can you make a small simple example in order to show what the output should be? – plonser Jun 22 '15 at 12:34
  • Are you working with numpy arrays or matrices? Similarly, should the output be an array or matrix? – Divakar Jun 22 '15 at 12:56
  • Suggest that you make sure you are clear on [the difference between numpy arrays and matrices](http://stackoverflow.com/questions/4151128/what-are-the-differences-between-numpy-arrays-and-matrices-which-one-should-i-u). – Lee Jun 22 '15 at 13:03
  • True, I wasn't sure if Matrix is needed, good to know both ways. – MarkokraM Jun 22 '15 at 14:20

4 Answers4

3

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]],
   .
   .
   .
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • I gave this thumbs up because of good variation of the wanted result, which I didn't actually presented on my poorly formed question. – MarkokraM Jun 22 '15 at 14:15
1
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

Kavin Eswaramoorthy
  • 1,595
  • 11
  • 19
0

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.

Lee
  • 29,398
  • 28
  • 117
  • 170
0

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)]
timakro
  • 1,739
  • 1
  • 15
  • 31