# Numpy sub-array assignment with advanced, mixed indexing

Looks like there are some errors in copying your code to question.

But I suspect there’s a known problem with indexing:

``````In : a=np.zeros((2,3,4)); b=np.ones((3,4)); I=np.array([0,1])
``````

Make `I` 2 elements. Indexing `b` gives the expected (3,2) shape. 3 rows from the slice, 2 columns from `I` indexing

``````In : b[:,I].shape
Out: (3, 2)
``````

But with 3d `a` we get the transpose.

``````In : a[0,:,I].shape
Out: (2, 3)
``````

and assignment would produce an error

``````In : b[:,I]=a[0,:,I]
...
ValueError: array is not broadcastable to correct shape
``````

It’s putting the 2 element dimension defined by `I` first, and the 3 element from `:` second. It’s a case of mixed advanced indexing that has been discussed earlier – and there’s a bug issue as well. (I’ll have to look those up).

You are probably using a newer `numpy` (or `scipy`) and getting a different error message.

It’s documented that indexing with two arrays or lists, and slice in the middle, puts the slice at the end, e.g.

``````In : a[[,,,],:,[0,1]].shape
Out: (4, 2, 3)
``````

The same thing is happening with `a[0,:,[0,1]]`. But there’s a good argument that it shouldn’t be this way.

As to a fix, you could transpose a value, or change the indexing

``````In : b[:,I]=a[0:1,:,I]

In : b[:,I]=a[0,:,I].T

In : b
Out:
array([[ 0.,  0.,  1.,  1.],
[ 0.,  0.,  1.,  1.],
[ 0.,  0.,  1.,  1.]])

In : b[:,I]=a[:,I]
``````

https://github.com/numpy/numpy/issues/7030

https://github.com/numpy/numpy/pull/6256