Indices


 * This page describes a primitive whose result is a list of indices. See Index for the page on indices themselves. See Index of for the index generator.

, or Where, is a monadic primitive function which returns the indices of all ones in a Boolean array. More generally, Indices accepts an array of non-negative integers and copies each index the corresponding number of times.

Where on a Boolean argument was introduced in K as, and it was extended to non-negative integers in J, with the name Indices.

Examples
In all implementations, Indices gives the indices of ones in a Boolean vector.

In nested APLs it returns nested indices when passed a matrix or higher-dimensional array.

If numbers higher than 1 are allowed, they indicate that the index of the number is repeated. Negative numbers are never allowed.

Description and APL model
Indices replicates each index in the argument by the number of times it appears. It is identical to the APL function:

The argument is restricted to be an array of non-negative integers, or, in Dyalog APL, Booleans.

Because Indices returns indices (like Iota), it is subject to index origin.

The only flat array language which implements Indices is J. Because J's Iota does not return multidimensional indices, J defines Indices to have function rank 1 so that only vector indices are used.

Mathematical interpretation
Indices may be viewed as a way to convert between two ways of writing multisets of array indices. The argument uses a dense representation indicating for each index the number of times it appears, and the result uses a sparse representation which lists all the indices contained in the set.

History
Idioms with similar behavior to Indices were widely used in APL long before it was made into a primitive. For example, the FinnAPL idiom library, first presented in 1984, lists  as "594. Indices of ones in logical vector X".

Where with a Boolean argument was present in K by K2 in 1998. It was extended to vectors of non-negative integers when it was added to J as Indices in release 5.02 (2003).

Indices was first introduced to APL, and the nested array model, by NARS2000. Originally defined only for vectors, the generalised definition  was introduced in about 2013 after some experimentation with alternatives. Where, with almost the same definition, was added to Dyalog APL 16.0 (June 2017), but with the restriction that the argument be Boolean, a restriction that was lifted to allow non-negative integers in 18.0 (2020). Dyalog's definition also returned  for   on a scalar , as opposed to NARS2000 which returned. In January 2018, NARS2000 switched to Dyalog's definition for scalar arguments;.

Lessons
APL Cultivation

Documentation
Dyalog

NARS2000

J Dictionary, NuVoc