Topic: APLX Help : Help on APL language : APL Fundamentals : Bracket indexing
[ Previous | Next | Contents | Index | APL Home ]

www.microapl.co.uk

Bracket indexing


Bracket indexing can be used to select elements from an array, for example one or more elements from a vector, or individual rows or columns of a matrix.

The index or indices are enclosed in square brackets, each dimension being separated by a semicolon. If no number is used for a particular dimension, then all the elements in that dimension are selected. APL allows index references to start either at 0 or 1. The index origin (which is controlled by ⎕IO or )ORIGIN) determines whether index positions start from 1 or 0. In the examples below, and generally throughout this manual, the default convention of index origin 1 is used.

             LIST←12 24 36 48
             LIST[2]                 (Selects the second item in LIST)
       24
             LIST[1]+LIST[4]         (Adds the first and fourth items in LIST)
       60
             ALF←'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
             ALF[26 1 13 2 9 1]      (Selects the letters in ZAMBIA
       ZAMBIA                         from the contents of ALF)
             TABLE                   (TABLE consists of 2 rows and 4 columns)
       10 20 30 40
       50 60 70 80
            TABLE[1;4]               (Selects the item in row 1, column 4)
       40
             TABLE[1;1 2 3 4]+TABLE[2;1 2 3 4]
       60 80 100 120                 (Adds the 4 columns in row 1 to the
                                      4 columns in row 2)
             TABLE[1;]+TABLE[2;]      (Shorthand way of doing the same
       60 80 100 120                  operation as in the last example)

In general, the indices may be of any shape or rank, so long as each of their elements correspond to valid elements within the array being indexed. The shape of the result of an indexing operation is generated by the shape of the index arrays. Thus

             ⍴TABLE[A;B]             (Where A and B are arrays)

is identical to

             (⍴A),⍴B

This has the important consequence, that if all the indexing arrays are in fact scalars, the result is also a scalar. Similarly, any axis of an array indexed by a scalar generates a result in which that axis does not exist.

             ALF[2 2⍴⍳4]             (ALF indexed by a matrix)
       AB                            (Result is a matrix)
       CD
             ⍴TABLE[1;1 2 3 4]       (Rows indexed by a scalar, result is
       4                              a vector)
             ⍴TABLE[,1;1 2 3 4]      (Rows indexed by a vector, result is
       1 4                            a matrix)
             ⍴TABLE[1 1⍴1;1 2 3 4]   (Rows indexed by a matrix,
       1 1 4                          result is a three dimensional array

The ('index') function

An alternative to bracket indexing is the ('index') function, which is discussed fully in the reference section. The index specification is given as the left argument to the function and is equivalent to bracket indexing in that

             ROW COL ⌷ MATRIX

and

             MATRIX[ROW;COL]

are equivalent. Although arguably less readable than bracket indexing, the index function has the advantage that it is syntactically consistent with other APL primitive functions, and can thus be used with operators such as Each.


Topic: APLX Help : Help on APL language : APL Fundamentals : Bracket indexing
[ Previous | Next | Contents | Index | APL Home ]