Hi, 2011/2/8 Alok Singhal <a...@merfinllc.com>: > Hi, > > I have an NxM array, which I am indexing with a 1-d, length N boolean > array. For example, with a 3x5 array: > > In [1]: import numpy > In [2]: data = numpy.arange(15) > In [3]: data.shape = 3, 5 > > Now, I want to select rows 0 and 2, so I can do: > > In [4]: mask = numpy.array([True, False, True]) > In [5]: data[mask] > Out[5]: > array([[ 0, 1, 2, 3, 4], > [10, 11, 12, 13, 14]]) > > But when the shape of 'data' is a 0xM, this indexing fails: > > In [6]: data2 = numpy.zeros((0, 5), 'd') > In [7]: mask2 = numpy.zeros(0, 'bool') > In [8]: data2[mask2] > ------------------------------------------------------------ > Traceback (most recent call last): > File "<ipython console>", line 1, in <module> > IndexError: invalid index > > I would have expected the above to give me a 0x5 array.
> Is there any other way to do what I > am doing? Like so (works only for ndim==2): >>> d1 = numpy.arange(0).reshape((0, 5)) >>> d2 = numpy.arange(15).reshape((3, 5)) >>> index1 = numpy.asarray([], dtype=numpy.bool) >>> index2 = numpy.asarray([True, False, True], dtype=numpy.bool) >>> x = numpy.arange(5) >>> (x1, y1) = numpy.meshgrid(x, index1.nonzero()[0]) >>> (x2, y2) = numpy.meshgrid(x, index2.nonzero()[0]) >>> (x1, y1) (array([], shape=(0, 5), dtype=int32), array([], shape=(0, 5), dtype=int32)) >>> print x2, "\n", y2 [[0 1 2 3 4] [0 1 2 3 4]] [[0 0 0 0 0] [2 2 2 2 2]] >>> d1[y1, x1] array([], shape=(0, 5), dtype=int32) >>> d2[y1, x1] array([], shape=(0, 5), dtype=int32) >>> d2[y2, x2] array([[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14]]) I don't know if the other thing is a bug, but it looks like. I could imagine that it has something to do with the implicit slicing on the array without data? Rather an imperfection ... Consider this: >>> d1 = numpy.arange(0).reshape((0,)) >>> d2 = numpy.arange(0).reshape((0, 5)) >>> d3 = numpy.arange(0).reshape((5, 0)) >>> d1[[]] array([], dtype=int32) >>> d2[[]] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: invalid index >>> d2[[], 0] array([], dtype=int32) >>> d3[[]] array([], shape=(0, 0), dtype=int32) >>> d3[0, []] array([], dtype=int32) >>> d3[:, []] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: invalid index Ticket? Friedrich _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion