index operator

Syntax

  • float/bool/string array[index]
  • float/bool/string array[rowIndex, colIndex]
  • float[]/bool[]/string[] array[indices]
  • float[]/bool[]/string[] array[rowIndex, colIndices]
  • float[]/bool[]/string[] array[rowIndices, colIndex]
  • float[]/bool[]/string[] array[rowIndices, colIndices]

Parameters

  1. array(float [], string[], bool[])
    Array for which an element or multiple elements are requested.
  2. index, rowIndex, colIndexfloat
    Zero-based linear/row/column array of array element.
  3. indices, rowIndices, colIndices—(float[], bool[])
    Array containing zero-based linear/row/column indices of array elements or logical values indicating whether a specific index is included.

Returns

An array element of a new array.

Description

Indexing with index values

The index operator returns the array element at a specific zero-based index position:

array =                                     [1,2,3,4] 

array[ 0 ]                                   1 

array[ 3 ]                                   4

or the element at a specific rowIndex (row index) and colIndex (column index):

array2d =                                    [1,2,3;
                                              4,5,6;
                                              7,8,9] 

array2d[ 1 , 1 ]                              5

Indexing with index arrays

Further, array elements can be indexed by an array (indices, rowIndices, colIndices). In this case the index operator returns a new array. Its dimensions are prescribed by the dimensions of the indices array.

array[ [0,2] ]                              [1,3] 

array[ [1,1] ]                              [2,2]
 
array[ 3:-1:1 ]                             [4,3,2] 

array[ [0,1;                                [1,2;
        2,3] ]                               3,4]
                                             
 
array2d[ 1 , [0,2] ]                        [4,6]
 
array2d[ [0,0,1] , 2 ]                      [3;
                                             3;
                                             6]
 
array2d[ 0:2 , [0,2] ]                      [1,3;
                                             4,6;
                                             7,9]

Logical indexing

An indices array (indices, rowIndices, colIndices) can also be given by logical values indicating whether a specific index is included:

array[ [true,false,true] ]                  [1,3] 

array[ array .> 2 ]                         [3,4] 

array[ array .> 1 .&& array .<= 3 ]         [2,3]
 
array2d[ [false,true,true], 0 ]             [4;
                                             7]

Invalid indexing

If an index is negative or greater than or equal to the number of elements or rows or columns respectively, a default value is returned. The default value is 0 for float arrays, false for bool arrays, and "" for string arrays.

array[ -1 ]                                  0 

array[ [false,false,true,true,true] ]       [3,4,0]
 
array2d[ [2,3], -1:1 ]                      [0,7,8;
                                             0,0,0]

Linear indexing

If a 2d array is indexed in 1d, linear indexing is used. Indexing is performed row-wise starting with elements in the first row and continuing on successive rows.

array2d[ 3 ]                                 4 

array2d[ 0 : size(array2d)-1 ]              [0,1,2,3,4,5,6,7,8,9] 

array2d[ [0;                                [1;
          3;                                 4;
          6] ]                               7]

Similarly, if a 2d indices array is used for row/column indexing, indices in rowIndices or colIndices are interpreted in a row-wise manner.

array2d[ 0 , [0,1;                          [1,2,3,0] 
              2,3] ]

Logical indexing is always linear.

array[ [true,false;                         [1,3] 
        true,false] ]

Related

Examples

Recursive element selection

With the size function and index operator an array can be recursively parsed for a specific element value. In this example the index of the longest edge is retrieved from an array of edge lengths.

indexOfLargest(array) = indexOfLargest(array, 0, 0)

indexOfLargest(array, i, iLargest) =
    case i == size(array) : iLargest
    else :
        case array[i] > array[iLargest] : indexOfLargest(array, i+1, i)
        else                            : indexOfLargest(array, i+1, iLargest)

const edgeLengths = comp(e) { all : scope.sx }

indexOfLongestEdge = indexOfLargest(edgeLengths)
                                        
Lot --> comp(e) { indexOfLongestEdge : LongestEdge. }

Parsing text file

A text file containing a table is parsed and elements are indexed linearly.

// table
// a;b;c↵
// d;e;f↵
// g;h;i

const file     = readTextFile("table.txt")
const cells    = splitString(file, "$;|\n")     // [a,b,c,d,e,f,g,h,i]
const columns  = 3
const indexes  = [0 : columns : size(cells)-1]  // [0:3:6]
const firstCol = cells[indexes]                 // [a,d,g]

Read CSV table

A CSV file is read and elements are indexed using the 2d index operator.

const table    = readStringTable("table.csv")
const firstCol = table[0 : nRows(table)-1, 0]     // [a,d,g]

Indexing by 2d arrays

The result of the 1d index operator is an array that has the same dimensions as the indices array.

const a = ["_", "d", "i", "a", "g"]

const b = [1,0,0,0;
           0,2,0,0;
           0,0,3,0;
           0,0,0,4]
		   
const c = a[b]
// (4x4)
//    d   _   _   _
//    _   i   _   _
//    _   _   a   _
//    _   _   _   g

Indexing out of bounds

An empty floatArray is indexed by index sequences. This creates a new array filled with default values (which is zero for float).

zeros(elems)    = floatArray[1:elems]
ones(rows,cols) = floatArray[1:rows,1:cols] .+ 1

const a = zeros(4)  // [0,0,0,0]
const b = ones(2,3) // [1,1,1 ; 1,1,1]

Prime numbers

An implementation of the Sieve of Eratosthenes that uses indexing by boolean arrays.

findPrimes(max) =
   case max <= 1 : floatArray
   else          : findPrimes(sqrt(max), [2 : max], floatArray)
				  
findPrimes(limit, a, primes) =
   case a[0] <= limit : findPrimes(limit, a[a .% a[0] .!= 0], [primes, a[0]])
   else               : [primes, a]
   
Lot --> print(findPrimes(20)) // (8)[2,3,5,7,11,13,17,19]