[Numpy-discussion] Addressing arrays

Brett Olsen brett.olsen@gmail....
Mon Jan 30 11:13:33 CST 2012


On Mon, Jan 30, 2012 at 10:57 AM, Ted To <rainexpected@theo.to> wrote:
> Sure thing.  To keep it simple suppose I have just a two dimensional
> array (time,output):
> [(1,2),(2,3),(3,4)]
> I would like to look at all values of output for which, for example time==2.
>
> My actual application has a six dimensional array and I'd like to look
> at the contents using one or more of the first three dimensions.
>
> Many thanks,
> Ted

Couldn't you just do something like this with boolean indexing:

In [1]: import numpy as np

In [2]: a = np.array([(1,2),(2,3),(3,4)])

In [3]: a
Out[3]:
array([[1, 2],
       [2, 3],
       [3, 4]])

In [4]: mask = a[:,0] == 2

In [5]: mask
Out[5]: array([False,  True, False], dtype=bool)

In [6]: a[mask,1]
Out[6]: array([3])

~Brett


More information about the NumPy-Discussion mailing list