SubsetMap

SubsetMap[f,{e1,e2,},{i,j,}]

yields an expression in which the elements ei, ej, in the list {e1,e2,} are replaced with the corresponding elements of the list obtained by evaluating f[{ei,ej,}].

SubsetMap[f,expr,{pos1,pos2,}]

replaces elements of expr at positions pos1, pos2, .

SubsetMap[f,{pos1,pos2,}]

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

Details

  • In SubsetMap[f,list,{i1,,in}], the function f takes a length-n sublist of list and is expected to return another list of length n, whose elements will replace those of the original sublist.

Examples

open allclose all

Basic Examples  (5)

Reverse elements at positions 2 and 4:

Extract, rotate and reinsert elements at positions 2, 4 and 5:

Extract, accumulate and reinsert elements at positions 2 to 5:

Reverse the elements of the diagonal of a matrix:

Accumulate the elements of the second column of a matrix:

Scope  (4)

Reverse the elements at even positions in a list:

Use equivalent notations for the position argument:

Act on ragged arrays:

An alternative notation:

Use SubsetMap as an operator form to show that these words are "even-letter palindromes":

Shift periodically the central elements of a list of colors:

Applications  (1)

Implement a sorting network that randomly sorts pairs of elements until all elements are sorted:

For longer lists, finding the last elements to reorder may take many steps:

Properties & Relations  (4)

Use SubsetMap to shift the positions of a subset of elements in a list:

This can be also compactly expressed with Cycles notation and Permute:

SubsetMap[f,expr,pos] is equivalent to MapAt[f,expr,pos] for a listable function f:

SubsetMap[f,expr,{}] returns expr:

SubsetMap[f,expr,All] is equivalent to f[expr]:

Possible Issues  (2)

If the list of positions is not canonically sorted, the result will inherit that order:

In SubsetMap[f,expr,{i,j,}], the list {i,j,} represents a list of positions, equivalent to {{i},{j},}:

Other functions using position specifications, like Extract, interpret a list of integers as a single position:

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

Text

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

CMS

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

APA

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

BibTeX

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

BibLaTeX

@online{reference.wolfram_2024_subsetmap, organization={Wolfram Research}, title={SubsetMap}, year={2019}, url={https://reference.wolfram.com/language/ref/SubsetMap.html}, note=[Accessed: 09-December-2024 ]}