⍳ — Indices / Index of
Function. Keys: Alt-i. Hanzi: 位. Ranks: 1 monadic, ∞ ∞ dyadic
⍳N generates 0…N-1. A negative N counts down, as J’s i. does. Exact input gives exact coordinates.
⍳4ₓ ⍝ [0 1 2 3]ₓ
⍳3ₓ ⍝ [0 1 2]ₓ
⍳¯3 ⍝ 2 1 0Range, ↦, counts from 1 instead, or between any two ends.
↦4 ⍝ 1 2 3 4
3↦6 ⍝ 3 4 5 6A shape vector generates an array of coordinate vectors. A negative length reverses its axis.
⍳2ₓ 2 ⍝ [[0 0] [0 1]⋄[1 0] [1 1]]
⍳2ₓ ¯2 ⍝ [[0 1] [0 0]⋄[1 1] [1 0]]A keyed shape names the result’s axes, as it does for reshape. The result is a named index, which pairs by name with axes of the same name. Indexing by a named index gives the result its name. •index gives an index whose name can’t collide with a caller’s names:
⍴⍳["i":3] ⍝ ["i":3]ₓ
x←10×⍳5ₓ ⋄ i←⍳["i":5]
x.[5|i+1] ⍝ ["i":5]⍴[10 20 30 40 0]On a matrix, ⍳ applies to each row, as Rank does. Fill pads the results to a common shape.
⍴⍳[2 3⋄1 1] ⍝ [2 2 3]ₓ⍳⍠A Y returns the keys of axis A, or 0…length-1 on an unkeyed axis. The result lies along an axis with the name of the axis it selects. It pairs with that axis by name in arithmetic and indexing. Indexing by it keeps the keys at the positions it selects. Several axes return a vector of selector vectors.
⍳₀["price":1 "qty":2] ⍝ "price" "qty"
⍳₁2 3⍴0 ⍝ [0 1 2]ₓ
⍴⍳⍠"j" ["i":2 "j":3]⍴0 ⍝ ["j":3]ₓOn an axis where only some positions have keys, ⍳⍠A Y gives the keys where present and positions elsewhere.
⍳₀["aa":10 20] ⍝ "aa" 1ₓX⍳Y finds the first matching major cell in X for each cell of Y. Not found: ≢X. Uses tolerant matching and returns exact positions.
"abc"⍳"cabz" ⍝ [2 0 1 3]ₓ
"abc"⍳'b' ⍝ 1ₓ
"abc"⍳⊂'b' ⍝ 1ₓA single query returns an atom. Batch axes supply the result shape. To search for an array as one item, enclose it with ⊂.
C←"cat" "dog"
C⍳⊂"dog" ⍝ 1ₓ
C⍳["dog"] ⍝ [1]ₓ
m←[10 20⋄30 40⋄50 60]
m⍳30 40 ⍝ 1ₓ
m⍳[30 40⋄] ⍝ [1]ₓ
m⍳[30 40⋄10 20] ⍝ [1 0]ₓInverse
⍳⁻Y gives the argument that generates Y: the count for a vector of positions, otherwise the shape. X⍳⁻Y gives the items of X at the positions Y. A position that wasn’t found is a DOMAIN error.
⍳⁻⍳5 ⍝ [5]ₓ
"abc"⍳⁻2 0 ⍝ "ca"