setElems function

Syntax

  • float[]/string[]/bool[] setElems(array, indices, values)
  • float[]/string[]/bool[] setElems(array, rowIndices, colIndices, values)

Parameters

  1. array—(float[], bool[], string[])
    Array for which elements are set.
  2. indices, rowIndices, colIndices—(float, float[], bool[])
    Zero-based index value or array of indices or array of logical values.
  3. values—(float, string, bool, float[], bool[], string[])
    Value or array of values to be set.

Returns

An array containing new element values.

Description

The setElems function sets elements of an array to new values. Elements are selected by index values, index arrays or logical arrays. Indexing follows the same rules as for the index operator (see therein for detailed description).

1d indexing

One element at a specific zero-based index position can be replaced by a new value. Several elements can be set at once to the new value by providing an index array.

array1d =                                   [0,0,0,0,0,0]
 
setElems(array1d, 2, 1)                     [0,0,1,0,0,0]
 
setElems(array1d, [1:4], 1)                 [0,1,1,1,1,0]

Elements selected by an index array can be replaced by a new values array. Both arrays must have the same size. Values are set at the index found at the respective position in the index array. Repeated indices override.

setElems(array1d, [1,3,5], [1:3])           [0,1,0,2,0,3]
 
setElems(array1d, [5,1,3], [1:3])           [0,2,0,3,0,1]
 
setElems(array1d, [1,1,1], [1:3])           [0,3,0,0,0,0]

2d indexing

2d arrays can be indexed by row indices and column indices.

array2d =                                   [0,0,0;
                                             0,0,0;
                                             0,0,0]
 
setElems(array2d, 1, 1, 5)                  [0,0,0;
                                             0,5,0;
                                             0,0,0]
 
setElems(array2d, [0,2], [0:2], 1)          [1,1,1;
                                             0,0,0;
                                             1,1,1]

A values array can be set. The values array must have the same dimensions (number of rows and columns) as specified by the size of the row index array and the size of the column index array. Values are set at the row index and column index found at the respective position in the row index array and column index array. Repeated indices override.

setElems(array2d, [1,2], [0,2], [1,2;3,4])  [0,0,0;
                                             1,0,2;
                                             3,0,4]
 
setElems(array2d, [2,1], [2,0], [1,2;3,4])  [0,0,0;
                                             4,0,3;
                                             2,0,1]
 
setElems(array2d, [1,1], [0,2], [1,2;3,4])  [0,0,0;
                                             3,0,4;
                                             0,0,0]

Logical indexing

Further, logical indexing can be used to select values.

sequence =                                  [1:7]
 
setElems(sequence, sequence .> 4, 4)        [1,2,3,4,4,4,4]

Value arrays must have the same size (or dimensions for 2d indexing) as the number of elements selected by the logical array.

setElems(sequence, sequence .> 4, [3,2,1])  [1,2,3,4,3,2,1]

Indexing out of bounds

Negative indexes are ignored, ie. respective values are not set. Indexes greater than or equal to the number of elements or rows or columns enlarge the array respectively. Missing elements are filled with default values.

array =                                     [1,1]
 
setElems(array, 3, 5)                       [1,1,0,5]
 
setElems(array, 2, 2, 5)                    [1,1,0;
                                             0,0,0;
                                             0,0,5]

Note:

The size of the returned array is limited. It can be configured in the Procedural Runtime preferences (Default: 100000).

Related

Example

Enumerate value

enumerate(array, value) with( found := array .== value )
   = setElems(floatArray, found, [1:sum(found)])
   
f = enumerate(["a","c","b","a","d","a"], "a") // [1,0,0,2,0,3]

Create diagonal matrix

diag(n) = setElems(floatArray[1:n,1:n], [0:n+1:n*n-1], 1)

f = diag(3) // [1,0,0;
            //  0,1,0;
            //  0,0,1]

Reshape array

reshape(array, rows, cols)
   = setElems(floatArray[1:rows,1:cols], [0:rows*cols-1], array)
   
array = [1,2,3;
         4,5,6]
		 
f = reshape(array, 3, 2) // [1,2;
                         //  3,4;
                         //  5,6]


In this topic