0

We have the following numpy array:

A = [[0,1,2,3],
[2,3,4,5],
[1,2,3,4],
[1,1,1,1],
[2,2,2,2],
[3,3,3,3],
[0,1,2,3],
[2,3,4,5],
[1,2,3,4],
 [10,10,10,10],
[20,20,20,20],
[30,30,30,30]]

I would like to create two new arrays:

B = [[0,1,2,3],
[2,3,4,5],
[1,2,3,4],
[0,1,2,3],
[2,3,4,5],
[1,2,3,4]]

and

C = [[1,1,1,1],
[2,2,2,2],
[3,3,3,3],
 [10,10,10,10],
[20,20,20,20],
[30,30,30,30]]

Basically I would like to split array A into two new arrays where B takes in groups of 3 rows, and array C takes the next groups of 3 rows.

2
  • create 2 functions f1 isIncreasingArray that takes an array and check if it's in increassing order by step 1. f2 isConstantArray checks if an array is constant. if both functions takes an array and return a bool you can use filter to get all the arrays that match that condition I could answer the question but I think that you might try solve it yourself Commented Nov 17, 2021 at 18:40
  • thank you for your comment, however it is not so clear to me: what exactly do you mean by checking if the array is constant? Commented Nov 17, 2021 at 18:47

2 Answers 2

2

There are probabily many different approaches to solve this, here is one:

import numpy as np
A = np.array([[0,1,2,3],
[2,3,4,5],
[1,2,3,4],
[1,1,1,1],
[2,2,2,2],
[3,3,3,3],
[0,1,2,3],
[2,3,4,5],
[1,2,3,4],
 [10,10,10,10],
[20,20,20,20],
[30,30,30,30]])

indices = np.reshape(np.arange(A.shape[0]),(-1,3))

B = A[indices[::2].flatten()]
C = A[indices[1::2].flatten()]
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your help!
2

Let's try:

tmp = A.reshape(-1, 3, A.shape[1])
B = tmp[::2]
C = tmp[1::2]

2 Comments

thank you for your help!
To get the output that is asked you have to do the following though: B.reshape(-1,4), C.reshape(-1,4)

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.