|
29. Array Indexing {(,⍺)[(⊂⍴⍺)⊥¨⊃∘.,/⍵]} From What is an Array? [91]:
Indexing on rectangular arrays can be modelled as follow:
ind←{(,⍺)[(⊂⍴⍺)⊥¨⊃∘.,/⍵]}
x←?5 7 11⍴100
i←?2 3⍴5 ⋄ j←?6⍴7 ⋄ k←?4 1 1⍴11
x[i;j;k] ≡ x ind i j k
1
The function {(⍴⍺)⊥⍵} is the inverse of the odometer function on ⍴⍺ . The cartesian product ∘.,/ and the inverse odometer can be combined for a slight gain in efficiency:
ind1←{(,⍺)[⊃∘.+/⍵×⌽×\⌽1↓(⍴⍺),1]}
Elided indices can be modelled by using '' to indicate elision or, if trailing, being elided altogether, and:
eix←{0⌷⍤1⊢(~r↑~⍵∊⊂'')⌽((r←≢⍴⍺)↑⍵),⍪⍳¨⍴⍺}
x[i;;k] ≡ x ind x eix i '' k
1
x[i;j;] ≡ x ind x eix i j
1
x[;j;] ≡ x ind x eix '' j
1
x[i;;] ≡ x ind x eix ⊂i
1
x[i;⍬;k] ≡ x ind x eix i ⍬ k ⍝ ⍬ different from ''
1
Based on
[49e].
|