2

I have two arrays, array A and B as:

import numpy as np
A = np.array(['A', 'B', 'C', 'D', 'E'])
B = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])

which I want to be mixed so that array B is placed in between A to give me an array C of the form:

C = [[ 'A', '1', 'B', '2', 'C', '3', 'D', '4', 'E', '5'],
     [ 'A', '6', 'B', '7', 'C', '8', 'D', '9', 'E', '10'],
     [ 'A', '11', 'B', '12', 'C', '13', 'D', '14', 'E', '15']]
1
  • What is the question? Commented Mar 18, 2015 at 5:01

1 Answer 1

2

You can use a combination of reshape (to expose the target axis) and concatenate (to join the arrays along this axis), with reshapeing back to the desired form:

import numpy as np
A = np.array(['A', 'B', 'C', 'D', 'E'])
B = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])

AA = np.tile(A, 3).reshape(3, 5, 1)
BB = B.reshape(3, 5, 1)

C = np.concatenate([AA, BB], axis=2).reshape(3, 10)

print(C)
Sign up to request clarification or add additional context in comments.

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.