# 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 [73]: 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 [74]: b[:,I].shape
Out[74]: (3, 2)
``````

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

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

and assignment would produce an error

``````In [76]: 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 [86]: a[[[0],[0],[1],[1]],:,[0,1]].shape
Out[86]: (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 [88]: b[:,I]=a[0:1,:,I]

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

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

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

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

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