0

Here is my question.

There is a matrix call 'dt'.

What I want to do is make new matrix with same dt[:,0].

Below example will be helpful to understand what I want to do.

ex.

dt = [[0,0,0,0]
      [0,0,1,444]
      [0,0,2,80]
      [0,0,3,5]
      [1,0,0,0]
      [1,0,1,444]
      [1,0,2,80]
      [1,0,4,75]
      [2,1,2,653]
      ...
      ]]


new_matrix_0 =
     [[0,0,0,0]
      [0,0,1,444]
      [0,0,2,80]
      [0,0,3,5]]

new_matrix_1 = 
    [[1,0,0,0]
     [1,0,1,444]
     [1,0,2,80]
     [1,0,4,75]]

I need a code between 'dt' >> 'new_matrix_()'.

Thanks.

dcfmkt
  • 25
  • 1
  • 3
  • use `np.where` to achieve the split – anurag Jan 15 '21 at 07:50
  • Does this answer your question? [Paritition array into N chunks with Numpy](https://stackoverflow.com/questions/14406567/paritition-array-into-n-chunks-with-numpy) – atin Jan 15 '21 at 08:00
  • I believe OP wants to split the array based on value of first column, if we look at the variable names they are explicit `new_matrix_0` for value `0` etc. – anurag Jan 15 '21 at 08:06

3 Answers3

0

You can just loop through dt and append the new matrix to a list.

def get_new_matrices(dt):
    new_matrices = []
    n_rows = len(dt)
    i = 0
    while i < n_rows:
        new_matrices.append(dt[:][i:i+4])
        i = i + 4
    new_matrices.append(dt[:][i-4:n_rows])
    return new_matrices


dt = [[0, 1, 2, 3],
      [4, 5, 6, 7],
      [0, 1, 2, 3],
      [4, 5, 6, 7],
      [0, 1, 2, 3],
      [4, 5, 6, 7],
      [0, 1, 2, 3],
      [4, 5, 6, 7]]
new_mats = get_new_matrices(dt)

You can also use np.array_split

dt = [[0, 1, 2, 3],
      [4, 5, 6, 7],
      [0, 1, 2, 3],
      [4, 5, 6, 7],
      [0, 1, 2, 3],
      [4, 5, 6, 7],
      [0, 1, 2, 3],
      [4, 5, 6, 7]]
n = len(dt)
x = np.array(dt)
new_mats = np.array_split(x, int(n/4))
atin
  • 985
  • 3
  • 11
  • 28
0

If you want to split the array into subarrays according to values in first column you can use split on indices where the values in the first column change. These indices can by found by diff and argwhere:

dt = [[0,0,0,0],
      [0,0,1,444],
      [0,0,2,80],
      [0,0,3,5],
      [1,0,0,0],
      [1,0,1,444],
      [1,0,2,80],
      [1,0,4,75],
      [2,1,2,653]]
a = np.array(dt)
idx = np.argwhere(np.diff(a[:,0])) + 1
np.split(a, idx.flat)

Result:

[array([[  0,   0,   0,   0],
        [  0,   0,   1, 444],
        [  0,   0,   2,  80],
        [  0,   0,   3,   5]]),
 array([[  1,   0,   0,   0],
        [  1,   0,   1, 444],
        [  1,   0,   2,  80],
        [  1,   0,   4,  75]]),
 array([[  2,   1,   2, 653]])]

(if you, however, just want to get the rows where the first column is equal to x then it's simply a[a[:,0]==x])

Stef
  • 28,728
  • 2
  • 24
  • 52
0

Assuming dt has 4*n, then you can split it in n arrays with np.split:

>>> dt
array([[  0,   0,   0,   0],
       [  0,   0,   1, 444],
       [  0,   0,   2,  80],
       [  0,   0,   3,   5],
       [  1,   0,   0,   0],
       [  1,   0,   1, 444],
       [  1,   0,   2,  80],
       [  1,   0,   4,  75],
       [  2,   1,   2, 653],
       [  4,   2,   1,   8],
       [ 10,  11,  21,   3],
       [  8,   5,   9,   1]])

>>> np.split(dt, len(dt)//4)
[array([[  0,   0,   0,   0],
        [  0,   0,   1, 444],
        [  0,   0,   2,  80],
        [  0,   0,   3,   5]]), 

 array([[  1,   0,   0,   0],
        [  1,   0,   1, 444],
        [  1,   0,   2,  80],
        [  1,   0,   4,  75]]), 

  array([[  2,   1,   2, 653],
        [  4,   2,   1,   8],
        [ 10,  11,  21,   3],
        [  8,   5,   9,   1]])]
Ivan
  • 34,531
  • 8
  • 55
  • 100