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.

4
  • Can you make a small simple example in order to show what the output should be? Commented Jun 22, 2015 at 12:34
  • Are you working with numpy arrays or matrices? Similarly, should the output be an array or matrix? Commented Jun 22, 2015 at 12:56
  • Suggest that you make sure you are clear on the difference between numpy arrays and matrices. Commented Jun 22, 2015 at 13:03
  • True, I wasn't sure if Matrix is needed, good to know both ways. Commented Jun 22, 2015 at 14:20

4 Answers 4

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]],
   .
   .
   .
Sign up to request clarification or add additional context in comments.

1 Comment

I gave this thumbs up because of good variation of the wanted result, which I didn't actually presented on my poorly formed question.
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

1 Comment

Thanks for paying attention to the first piece of output cell. This is what I was after.
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)]

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.