Indices: Difference between revisions
Miraheze>Marshall m (→Examples) |
Miraheze>Marshall m (→Examples) |
||
Line 12: | Line 12: | ||
3 7 | 3 7 | ||
</source> | </source> | ||
{{Works in|[[Dyalog APL]],[[dzaima/APL]],[[NARS2000]]}} | {{Works in|[[Dyalog APL]], [[dzaima/APL]], [[NARS2000]]}} | ||
In [[Nested array model|nested]] APLs it returns nested indices when passed a [[matrix]] or higher-dimensional array. | In [[Nested array model|nested]] APLs it returns nested indices when passed a [[matrix]] or higher-dimensional array. | ||
Line 25: | Line 25: | ||
└┘ | └┘ | ||
</source> | </source> | ||
{{Works in|[[Dyalog APL]],[[dzaima/APL]],[[NARS2000]]}} | {{Works in|[[Dyalog APL]], [[dzaima/APL]], [[NARS2000]]}} | ||
If numbers higher than 1 are allowed, they indicate that the index of the number is repeated. Negative numbers are never allowed. | If numbers higher than 1 are allowed, they indicate that the index of the number is repeated. Negative numbers are never allowed. | ||
Line 32: | Line 32: | ||
1 1 1 3 3 | 1 1 1 3 3 | ||
</source> | </source> | ||
{{Works in|[[dzaima/APL]],[[NARS2000]]}} | {{Works in|[[dzaima/APL]], [[NARS2000]]}} | ||
== Description and APL model == | == Description and APL model == |
Revision as of 14:29, 24 October 2019
This function describes a primitive whose result is a list of indices. See Index for the page on indices themselves.
Indices (⍸
), 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.
Indices was introduced in J as I.
and is present in K as &
.
Examples
In all implementations, Indices gives the indices of ones in a boolean vector.
⍸ 0 0 1 0 0 0 1 0 3 7
In nested APLs it returns nested indices when passed a matrix or higher-dimensional array.
⍸ 3 3⍴0 0 1 0 0 0 1 0 ┌───┬───┐ │1 3│3 1│ └───┴───┘ ⍸1 ⍝ An index into a scalar is empty! ┌┐ ││ └┘
If numbers higher than 1 are allowed, they indicate that the index of the number is repeated. Negative numbers are never allowed.
⍸ 3 0 2 1 1 1 3 3
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:
Where ← {(,⍵)⌿,⍳⍴⍵}
The argument is restricted to be an array of non-negative integers, or, in Dyalog APL, booleans.
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.
Documentation
J Dictionary, NuVoc