2

Lets say I make the following arrays:

>>> a = zeros((2,2,2))
>>> b = ones((2,2))

How can I insert b into the middle of a? So my resulting array will look like

array([[[ 0.,  0.],
        [ 0.,  0.]],

       [[ 1.,  1.],
        [ 1.,  1.]],

       [[ 0.,  0.],
        [ 0.,  0.]]])
2
  • How does a 2x2x2 array end up with 12 elements? It should only have 8 elements. Where do you want to insert the array? How do you define "middle"? Commented Apr 4, 2018 at 20:40
  • please comment what you have tried Commented Apr 4, 2018 at 20:42

3 Answers 3

3

You can use numpy.insert():

a = np.insert(a, 1, b, axis=0)
Sign up to request clarification or add additional context in comments.

1 Comment

ahh, i had tried this but didn't putthe axis = 0 at the end
0

A way to show same as your result is using tuple. code:

b = numpy.ones((2,2))
a = numpy.zeros((2, 2))
tpl = a,
tpl = b,
tpl = a,

So you can consider it as 3dArray and also you can use for loop to add multi array into it.Hope be helpfull.

Comments

-1
a=np.zeros((3,2,2))
b=np.ones((2,2))

a[1]=b*1

That will produce your output.

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.