APLX Help : Help on APL language : APL Fundamentals : Bracket indexing


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 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


APLX Help : Help on APL language : APL Fundamentals : Bracket indexing

Copyright © 19962010 MicroAPL Ltd