WOLFRAM

MapAt[f,expr,n]

applies f to the element at position n in expr. If n is negative, the position is counted from the end.

MapAt[f,expr,{i,j,}]

applies f to the part of expr at position {i,j,}.

MapAt[f,expr,{{i1,j1,},{i2,j2,},}]

applies f to parts of expr at several positions.

MapAt[f,pos]

represents an operator form of MapAt that can be applied to an expression.

Details

  • MapAt[f,expr,{i,j,}] or MapAt[f,expr,{{i,j,}}] applies f to the part expr[[i,j,]].
  • MapAt[f,expr,{{i1,j1,},{i2,j2,},}] applies f to parts expr[[i1,j1,]], expr[[i2,j2,]], . »
  • The list of positions used by MapAt is in the same form as is returned by the function Position. »
  • MapAt works on Association objects, using the same specification for keys as in Part.
  • MapAt applies f repeatedly to a particular part if that part is mentioned more than once in the list of positions. »
  • MapAt[f,pos][expr] is equivalent to MapAt[f,expr,pos].

Examples

open allclose all

Basic Examples  (8)Summary of the most common use cases

Map f onto the part at position 2:

Out[1]=1

Map f onto multiple parts:

Out[1]=1

Map f onto a more deeply nested part:

Out[1]=1

Map f onto the second element of all top-level parts (the "second column"):

Out[1]=1

Map f onto an association:

Out[1]=1

Use Key to specify position:

Out[1]=1

For string keys, Key is not needed:

Out[2]=2

Use negative position in an association:

Out[1]=1

Use the operator form of MapAt:

Out[1]=1

Scope  (9)Survey of the scope of standard use cases

Part Specifications  (9)

A single element of the expression:

Out[1]=1

Negative numbers count from the end:

Out[1]=1

Map at a part at level 2:

Out[1]=1

Map at several elements:

Out[1]=1

Map at several nested parts:

Out[1]=1

Map using Span syntax:

Out[1]=1

Map f at a more deeply nested part:

Out[1]=1

Map f at several locations:

Out[1]=1

Map f at several levels:

Out[1]=1

Generalizations & Extensions  (4)Generalized and extended use cases

MapAt can be used on expressions with any head:

Out[1]=1
Out[2]=2

Key and integer part specifications in associations are different:

Out[1]=1

The function can be mapped onto the heads as well:

Out[1]=1

Map can be used on sparse arrays:

Out[1]=1
Out[2]=2

Applications  (4)Sample problems that can be solved with this function

Reset an element in a matrix:

Out[1]=1

Reverse the sign of an element in a matrix:

Out[1]=1

Enumerate cases with f mapped onto successive elements:

Out[1]=1

Reverse the color of a cell at a random position at each step:

Out[1]=1

Properties & Relations  (2)Properties of the function, and connections to other functions

MapAt can use lists of parts returned by Position:

Out[2]=2
Out[3]=3

These are the subexpressions returned by Extract:

Out[4]=4

MapAt applies f repeatedly if a part is mentioned repeatedly:

Out[1]=1

Possible Issues  (1)Common pitfalls and unexpected behavior

MapAt[f,expr,{}] does not map f because the list of positions is empty:

Out[1]=1

MapAt[f,expr,{{}}] maps f to position {}, which corresponds to the whole expression expr:

Out[2]=2
Wolfram Research (1988), MapAt, Wolfram Language function, https://reference.wolfram.com/language/ref/MapAt.html (updated 2014).
Wolfram Research (1988), MapAt, Wolfram Language function, https://reference.wolfram.com/language/ref/MapAt.html (updated 2014).

Text

Wolfram Research (1988), MapAt, Wolfram Language function, https://reference.wolfram.com/language/ref/MapAt.html (updated 2014).

Wolfram Research (1988), MapAt, Wolfram Language function, https://reference.wolfram.com/language/ref/MapAt.html (updated 2014).

CMS

Wolfram Language. 1988. "MapAt." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/MapAt.html.

Wolfram Language. 1988. "MapAt." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/MapAt.html.

APA

Wolfram Language. (1988). MapAt. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MapAt.html

Wolfram Language. (1988). MapAt. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/MapAt.html

BibTeX

@misc{reference.wolfram_2024_mapat, author="Wolfram Research", title="{MapAt}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/MapAt.html}", note=[Accessed: 22-December-2024 ]}

@misc{reference.wolfram_2024_mapat, author="Wolfram Research", title="{MapAt}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/MapAt.html}", note=[Accessed: 22-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_mapat, organization={Wolfram Research}, title={MapAt}, year={2014}, url={https://reference.wolfram.com/language/ref/MapAt.html}, note=[Accessed: 22-December-2024 ]}

@online{reference.wolfram_2024_mapat, organization={Wolfram Research}, title={MapAt}, year={2014}, url={https://reference.wolfram.com/language/ref/MapAt.html}, note=[Accessed: 22-December-2024 ]}