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:

Introduced in 2019
 (12.0)