[Numpy-discussion] Logical indexing and higher-dimensional arrays.

josef.pktd@gmai... josef.pktd@gmai...
Wed Feb 8 11:17:42 CST 2012


On Wed, Feb 8, 2012 at 10:29 AM, Sturla Molden <sturla@molden.no> wrote:
> On 08.02.2012 15:49, Travis Oliphant wrote:
>
>> This sort of thing would take time, but is not out of the question in my mind because I suspect the number of users and use-cases of "broadcasted" fancy-indexing is small.

I think I use it quite a bit, and I like that the broadcasting in
indexing is as flexible as the broadcasting of numpy arrays
themselves.

x[np.arange(len(x)), np.arange(len(x))]  gives the diagonal for example.

or picking a different element from each column, (I don't remember
where I used that)

It is surprising at first and takes some getting used to, but I think
it's pretty nice.  On the other hand, I always avoid mixing slices and
indexes because of "strange" results.

Josef


>
> In Matlab this (misfeature?) is generally used to compensate for the
> lack of broadbasting. So many Matlab programmers manually broadcast by
> fancy indexing with an array of (boolean) ones (it's less verbose than
> reshape). But with broadcasting for binary array operators (NumPy and
> Fortran 90), this is never needed.
>
> Sturla
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion


More information about the NumPy-Discussion mailing list