0

I've got problem with reshaping simple 2-d array into another. Let`s assume matrix :

[[4 1 2  1 2 4  1 2 4]
 [2 3 0  3 0 2  3 0 2]
 [5 5 1  5 1 5  5 1 5]
 [6 6 6  6 6 6  6 6 6]]

What I want to do is to reshape it to (12, 3) matrix, but using (4, 3) block. What I meant to do is to get matrix like:

[[4 1 2
  2 3 0
  5 5 1
  6 6 6

  1 2 4
  3 0 2
  5 1 5
  6 6 6

  1 2 4
  3 0 2
  5 1 5
  6 6 6]]

I have highlighted the "egde" of cutting this matrix by additional newline. I`ve tried numpy reshape (with all available order parameter value), but still I get array with "mixed" values.

1 Answers1

0

You can always do this manually for custom reshapes:

import numpy as np

data = [[4, 1, 2, 1, 2, 4, 1, 2, 4],
        [2, 3, 0, 3, 0, 2, 3, 0, 2],
        [5, 5, 1, 5, 1, 5, 5, 1, 5],
        [6, 6, 6, 6, 6, 6, 6, 6, 6]]


X = np.array(data)

Z = np.r_[X[:, 0:3], X[:, 3:6], X[:, 6:9]]
print(Z)

yields

array([[4, 1, 2],
       [2, 3, 0],
       [5, 5, 1],
       [6, 6, 6],
       [1, 2, 4],
       [3, 0, 2],
       [5, 1, 5],
       [6, 6, 6],
       [1, 2, 4],
       [3, 0, 2],
       [5, 1, 5],
       [6, 6, 6]])

note the special np.r_ operator that concatenates arrays on rows (first axis). It is just a handy alias for np.concatenate.

smarie
  • 4,568
  • 24
  • 39