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.
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))