# Command: MapAt

NEST HelpDesk | Command Index | NEST Quick Reference

Name:

    MapAt - applies a function to some of the elements of its argument

Synopsis:

    array1 proc array2 MapAt -> array3

Examples:

      [3 4 5 6 7] {dup mul} -2 MapAt
      -> [3 4 5 36 7]
      [3 [-9 -12] 5 6 7] {dup mul} [2 2] MapAt
      -> [3 [-9 144] 5 6 7]
      [3 4 5 6 7] {dup mul} [[1] [3]] MapAt
      -> [9 4 25 6 7]
      [[3 9] 4 [5 -11] 6 7] {dup mul} [[1 2] [3 1]] MapAt
      -> [[3 81] 4 [25 -11] 6 7]

      [0 0 0 0 0] {1 add} [2 4 5 2 3 2 2 5] 1 1 Partition MapAt
      -> [0 4 1 1 2]

Description:

      MapAt successively applies proc to the elements of array1
      specified by array2 and replaces the original values by the
      return value of proc. The return value array3 has exactly the
      same shape as the first argument array1.

      Compared to languages like Matlab MapAt constitutes an lhs
      assignment operator for constructs like a(i)=f(a(i)), where
      i may be an array of indices. However, unlike in Matlab no
      temporary object a(i) for the rhs expression is created.
      Consequently, if the index ii occurs in array i n times the
      final value of a(ii) returned by MapAt is the cumulative effect
      of f operating n times on the original value of a(ii):
      a(ii) <- f(f(...f(a(ii))...))
      n times
      In Matlab the result is f(a(ii)), independent of n.
      The behavior of MapAt is, for example, useful in counting
      processes like the construction of a histogram as shown in the
      last example of the examples section.

Parameters:

      array1 is an arbitrarily shaped array. In particular
      it does not need to be rectangular.
      array2 specifies a multi-dimensional position [i, j,...]
      in array1 or a list of positions [ [i1,j1,...], [i2,j2,...], ...].
      The same element may be specified multiple times in array2 at
      arbitrary positions.

      The first element on each level has index 1. Indices can also
      be specified counting from the end of the array, in this case the
      last element has index -1. Positive and negative indices can
      arbitrarily be intermixed.

Remarks:

      This function is an implementation of Mathematica's MapAt function.
      Mathematica-style functions in SLI use Mathematica index notation.

References:

      [1] The Mathematica Book V4.0 "Part"

Author:

    Diesmann

FirstVersion:

    2007.08.12

SeeAlso:

- ReplacePart
- Part
- Map
- Partition

Source:

    /opt/conda/conda-bld/nest_1524088722951/work/nest-2.14.0/lib/sli/mathematica.sli

NEST HelpDesk | Command Index | NEST Quick Reference

© 2004 The NEST Initiative