# [Numpy-discussion] Fancy indexing question:

Christopher Barker Chris.Barker@noaa....
Tue Feb 24 14:06:05 CST 2009

```Robert Kern wrote:
> On Tue, Feb 24, 2009 at 13:39, Christopher Barker <Chris.Barker@noaa.gov> wrote:
>>  >>> a
>> array([[ 0,  1,  2,  3],
>>        [ 4,  5,  6,  7],
>>        [ 8,  9, 10, 11],
>>        [12, 13, 14, 15],
>>        [16, 17, 18, 19],
>>        [20, 21, 22, 23]])
>>
>> I want to extract a sub-array:

>> [[ 5,  7],
>>  [13, 15],
>>  [17, 19]]

>
>   http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

well, I did google a fair bit, though, oddly, I didn't find that page.

> The short answer is that in multidimensional fancy indexing, the index
> arrays are broadcasted against each other first, then the result array
> is found by iterating over the broadcasted arrays in parallel.

I did read this description before, and really didn't get it, or how to
apply it to this problem. However, with your prodding, I thought about
it some more, and I think I got it.

I suppose I should figure out how to contribute to the docs, but here is
how I came to understand it, in the context of my example:

I want to extract a 3x2 subarray, with the 3 rows specified by i, and
the two columns specified by j.

"""
All the integer indexing arrays must be broadcastable to the same shape.

The shape of the output (or the needed shape of the object to be used
for setting) is the broadcasted shape.
"""

OK -- so I need my index arrays to broadcast to a 3x2 array -- this was
my "light bulb" moment. I need to make the i index array a column:

>>> i = np.array((1,3,4)).reshape((-1,1))

so now they will broadcast to a rectangle:

>>> i * j
array([[ 1,  3],
[ 3,  9],
[ 4, 12]])

and the indexing works like I want:

>>> a[i,j]
array([[ 5,  7],
[13, 15],
[17, 19]])

This makes so much sense when I think of it this way:

to extract a particular set of rows, I need a column of indexes:

a:

[[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])

I want to extract a sub-array:

>> [[ 5,  7],
>>  [13, 15],
>>  [17, 19]]

which are these columns:

1       3

[[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])

and these rows:

[[ 0,  1,  2,  3],
1   [ 4,  5,  6,  7],
[ 8,  9, 10, 11],
3   [12, 13, 14, 15],
4   [16, 17, 18, 19],
[20, 21, 22, 23]])

so clearly, I need a (n,1) array to index the rows.

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov
```