0

I want to insert a numpy array A of size 508x12 into another numpy array B of size 508x13 resulting in an array of size 508x25. But here is the thing, i don't just want to concatanete them, but insted insert the array at one specific column location c. enter image description here

How would I do that?, I have tried:

C = np.insert(B, c, A, axis=1)
1
  • 2
    Hello! I am not too sure if this is possible with insert(), maybe someone will bring up a better answer. But in the meantime, I'd suggest that you can relatively safely use something like C = np.concatenate([B[:, 0:c], A, B[:, c:]], axis=1), in case this helps. I hope I got the dimensions right. Commented Nov 9, 2019 at 23:00

1 Answer 1

1

Just split up the concatenation like @brezniczky suggested. Alternatively, use hstack:

import numpy as np


a = np.ones((508,12))
b = np.zeros((508,13))

col = 3

final = np.hstack((b[:,0:col],a,b[:,col:])) 

print(final[0])

[0. 0. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

In this scenario hstack and concatenation with axis=1 are the same, I just prefer hstack for better readability

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.