ReplaceAt

ReplaceAt[expr,rules,n]

transforms expr by replacing the n^(th) element using rules.

ReplaceAt[expr,rules,{i,j,}]

replaces the part of expr at position {i,j,}.

ReplaceAt[expr,rules,{{i1,j1,},{i2,j2,},}]

replaces parts at several positions.

ReplaceAt[rules,pos]

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

Details

  • ReplaceAt is typically used to change one or more elements of an expression, based on supplied replacement rules.
  • ReplaceAt[expr,rules,{i,j,}] replaces the part expr[[i,j,]] using rules.
  • ReplaceAt[expr,rules,{{i1,j1,},{i2,j2,},}] replaces the parts expr[[i1,j1,]], expr[[i2,j2,]], using rules.
  • The rules must be of the form lhs->rhs or lhs:>rhs, or a list of such rules.
  • A list of rules can be given. The rules are tried in order. The result of the first one that applies is returned. If none of the rules apply, the original expr is returned.
  • ReplaceAt[expr,patt:>new,pos] can be used to replace a part without first evaluating it. With a rule such as pattnew, new is evaluated separately for each expression that matches patt.
  • ReplaceAt applies replacement rules repeatedly to a particular part if that part is mentioned more than once in the list of positions.
  • The list of positions used by ReplaceAt is in the same form as is returned by the function Position.
  • ReplaceAt works on Association objects, using the same specification for keys as in Part.
  • If expr is a SparseArray object or structured array, ReplaceAt[expr,] replaces parts in the corresponding ordinary array.
  • ReplaceAt[rules,pos][expr] is equivalent to ReplaceAt[expr,rules,pos].

Examples

open allclose all

Basic Examples  (9)

Replace a specified part of an expression using a rule:

Replace multiple parts:

Replace a nested part:

Use negative positions to count from the end:

Use pattern-based replacements:

Use multiple replacement rules:

Replace the second element of all top-level parts (the second column):

Replace parts of an association specified by Key:

Use the operator form of ReplaceAt:

Scope  (6)

A single element of the expression:

Negative numbers count from the end:

Replace parts at level 2:

Replace several parts:

Replace several nested parts:

Use Span syntax:

Generalizations & Extensions  (4)

ReplaceAt can be used on expressions with any head:

Key and integer part specifications in associations are different:

Use part 0 to perform replacements in the head of an expression:

ReplaceAt can be used with sparse arrays:

Properties & Relations  (7)

ReplaceAt can use lists of parts returned by Position:

These are the subexpressions returned by Extract:

ReplaceAt can use an Association for replacement rules:

ReplaceAt can use a Dispatch object for replacement rules:

ReplaceAt applies replacement rules repeatedly if a part is mentioned repeatedly:

ReplaceAt will not evaluate held replacements:

Manipulate code in an unevaluated association:

Evaluate upon retrieval:

Use Unevaluated to perform replacements prior to evaluating expr:

Possible Issues  (1)

Repeated replacement parts may produce unexpected results if the structure of the expression is expected to change during replacements:

Compare to using Nest:

This behavior is consistent with ReplacePart and MapAt:

Wolfram Research (2022), ReplaceAt, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplaceAt.html.

Text

Wolfram Research (2022), ReplaceAt, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplaceAt.html.

CMS

Wolfram Language. 2022. "ReplaceAt." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ReplaceAt.html.

APA

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

BibTeX

@misc{reference.wolfram_2024_replaceat, author="Wolfram Research", title="{ReplaceAt}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/ReplaceAt.html}", note=[Accessed: 21-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_replaceat, organization={Wolfram Research}, title={ReplaceAt}, year={2022}, url={https://reference.wolfram.com/language/ref/ReplaceAt.html}, note=[Accessed: 21-November-2024 ]}