Arrays used as subscripts have special meanings which implicitly invoke the
functions put (page x[ind1, ind2]
forms a new array with the same shape
as ind1, ind2 (they all must be broadcastable to the same shape)
and values such: "result[i,j,k] = x[ind1[i,j,k], ind2[i,j,k]]" In this
example, ind1, ind2 are index arrays with 3 dimensions (but they
could have an arbitrary number of dimensions). To illustrate with some
specific examples:
>>> x=2*arange(10) >>> ind1=[0,4,3,7] >>> x[ind1] array([ 0, 8, 6, 14]) >>> ind1=[[0,4],[3,7]] >>> x[ind1] array([[ 0, 8], [ 6, 14]])
>>> x=reshape(arange(12),(3,4)) >>> x array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> ind1=array([2,1]) >>> ind2=array([0,3]) >>> x[ind1,ind2] array([8, 7])
>>> ind1=array([[2,2],[1,0]]) >>> ind2=array([[0,1],[3,2]]) >>> x[ind1,ind2] array([[8, 9], [7, 2]])
>>> ind1.shape=(4,) >>> ind2.shape=(4,) >>> x[ind1,ind2] array([8, 9, 7, 2])
Notice this is the same values in the same order, but now as a one-d array. One index array does a broadcast:
>>> x[ind1] array([[ 8, 9, 10, 11], [ 8, 9, 10, 11], [ 4, 5, 6, 7], [ 0, 1, 2, 3]]) >>> ind1.shape=(2,2) >>> x[ind1] array([[[ 8, 9, 10, 11], [ 8, 9, 10, 11]], [[ 4, 5, 6, 7], [ 0, 1, 2, 3]]])
Again, note that the same 'elements', in this case rows of x, are returned in both cases. But in the second case, ind1 had two dimensions, and so using it to index only one dimension of a two-d array results in a three-d output of shape (2,2,4); i.e., a 2 by 2 'array' of 4-element rows.
When using constants for some of the index positions, then the result uses that constant for all values. Slices and strides (at least initially) will not be permitted in the same subscript as index arrays. So
>>> x[ind1, 2] array([[10, 10], [ 6, 2]])
>>> x[ind1, 1:3] Traceback (most recent call last): ... IndexError: Cannot mix arrays and slices as indices
x[ind1, ind2, ind3] = values
x[ind1[i,j,k], ind2[i,j,k], ind3[i,j,k]] = values[i,j,k]
The index arrays and the value array must be broadcast consistently. (As an
example: ind1.setshape((5,4))
, ind2.setshape((5,))
,
ind3.setshape((1,4))
, and values.setshape((1,))
.)
>>> x=zeros((10,10)) >>> x[[2,5,6],array([0,1,9,3])[:,NewAxis]]=array([1,2,3,4])[:,NewAxis] >>> x array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2, 0, 4, 0, 0, 0, 0, 0, 3], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 2, 0, 4, 0, 0, 0, 0, 0, 3], [1, 2, 0, 4, 0, 0, 0, 0, 0, 3], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
Send comments to the NumArray community.