3A. Indexing

encompasses everything performed by bracket indexing in other languages. The ranks are 0 _  , that is, x{y can be considered individually for each scalar in x and for y in toto, with the overall result constructed from the individual results in the same way as for all other functions. For scalar x then:

>x is a scalar or a list, and r=:>j{,>x  are the indices for axis j . r may be integers in the range i.&.(+&n)n=:j{$y ,  which selects cells n|r , or r  may be boxed integers in that range, in which case the selected cells are all except those in n|r . Thus:

   z=: 0{y=: 3 3 3$'ABCDEFGHIJKLMNOPQRSTUVWXYZ]'
   ibbb=: <ibb=: <ib=: <i=: 1 _1
   jbbb=: <jbb=: <jb=: <j=: 2 1
   ijbbb=: <ijbb=: <ijb=: <ij=: 2 2$i,j
   (] ; i&{ ; ib&{ ; ibb&{ ; ibbb&{) z
+---+---+-+---+---+
|ABC|DEF|F|DEF|ABC|
|DEF|GHI| |GHI|   |
|GHI|   | |   |   |
+---+---+-+---+---+

   ijb{y
|rank error
|   ijb    {y

   (] ; i&{ ; ijbb&{ ; ijbbb&{) z
+---+---+---+---+
|ABC|DEF|DEF|ABC|
|DEF|GHI|GHI|   |
|GHI|   |   |   |
|   |   |GHI|   |
|   |   |DEF|   |
+---+---+---+---+

The amend adverb } applied to an index produces a function that replaces the selected part of the right argument by the left argument. For example:

   '*' ib} z
ABC
DE*
GHI

   ('def',:'ghi') i} z
ABC
def
ghi

      (] ; i&{ ; ib&{ ; ibb&{ ; ibbb&{)"2 y
+---+---+-+---+---+
|ABC|DEF|F|DEF|ABC|
|DEF|GHI| |GHI|   |
|GHI|   | |   |   |
+---+---+-+---+---+
|JKL|MNO|O|MNO|JKL|
|MNO|PQR| |PQR|   |
|PQR|   | |   |   |
+---+---+-+---+---+
|STU|VWX|X|VWX|STU|
|VWX|YZ]| |YZ]|   |
|YZ]|   | |   |   |
+---+---+-+---+---+

   $(<<'') { z
0 3

   $(<a:) { z
0 3

Indexing on higher-rank arrays may be illustrated by the argument y :

   ]k=: <1 2;a:;0 2
+------------+
|+---+--+---+|
||1 2|++|0 2||
||   ||||   ||
||   |++|   ||
|+---+--+---+|
+------------+

 
   y ; k{y
+---+--+
|ABC|JL|
|DEF|MO|
|GHI|PR|
|   |  |
|JKL|SU|
|MNO|VX|
|PQR|Y]|
|   |  |
|STU|  |
|VWX|  |
|YZ]|  |
+---+--+

The following examples further illustrate the use of the indexing function. For each example, it may be instructive to plug the values into the expression r=:>j{,>x and work out the result.

n0=: y=: i.4 5 6 7

Array used in examples

n1=: (<,<3){y

Item 3 of y

n2=: (<,3){y

Item 3 of y

n3=: (<3){y

Item 3 of y

n4=: 3{y

Item 3 of y

n5=: (<,<_1){y

The last item of y (item _1 of y)

n6=: (<,_1){y

The last item of y (shape 5 6 7)

n7=: (<_1){y

The last item of y

n8=: _1{y

The last item of y

n9=: (_1+#y){y

The last item of y

n10=: 0{y

The first item of y

n11=: (-#y){y

The first item of y

n12=: 3 0 _2 0{y

Items 3 0 _2 0 of y

n13=: i=: ?2 3$0{$y

Indices used in examples

n14=: j=: ?    1{$y

Indices used in examples

n15=: k=: ?7  $2{$y

Indices used in examples

n16=: (<i;j;k){y

y[i;j;k;]in APL notation

n17=: (<1;2;3){y

y[1;2;3;]

n18=: (<1,2,3){y

y[1;2;3;]

n19=: (<1 2 3){y

y[1;2;3;]

n20=: (<<i){y

y[i;;;;]

n21=: (<<,i){y

y[,i;;...;]

n22=: (,i){y

y[,i;;...;]

n23=: (<<1 3 2){y

Items 1 3 2

n24=: (<<<1 3 2){y

All but items 1 3 2

n25=: (<<<1 3){y

All but items 1 3

n26=: (<<<1){y

All but items 1

n27=: (<<<$0){y

All but items ... none; i.e. all items

n28=: (<<a:){y

All items

n29=: (<1 3 2;3){y

y[1 3 2;3;;...;]in APL (0-origin)

n30=: (<(<1 3 2);3){y

y[(i.#y)-.1 3 2;3;;...;]

n31=: (<(<1 3);3){y

y[(i.#y)-.1 3;3;;...;]

n32=: (<(<1);3){y

y[(i.#y)-.1;3;;...;]

n33=: (<(<$0);3){y

y[(i.#y)-.$0;3;;...;]

n34=: (<(<$0);3){y

y[;3;;...;]

n35=: (<a:;3){y

y[;3;;...;]

n36=: 4{"_1 y

y[;4;;...;]

n37=: (<a:;a:;5){y

y[;;5;...;]

n38=: 5{"_2 y

y[;;5;...;]

n39=: (<1 2){y

Abbreviated (fewer indices than axes)

n40=: _2{y

Negative

n41=: (<<<3){y

Complementary

n42=: (1 2;3 2;0 _2){y

Scattered (non-scalar left argument)

JPhrases/Indexing (last edited 2009-09-02 00:05:33 by RicSherlock)