MapIndexed
[f, expr]MapIndexed
[f, expr, levelspec]MapIndexed[f, {a, b, c}]
Include heads (index 0):
MapIndexed[f, {a, b, c}, Heads->True]
Map on levels 0 through 1 (outer expression gets index {}
):
MapIndexed[f, a + b + c * d, {0, 1}]
Get the positions of atoms in an expression (convert operations to List
first
to disable Listable
functions):
expr = a + b * f[g] * c ^ e;
listified = Apply[List, expr, {0, Infinity}];
MapIndexed[#2 &, listified, {-1}]
Replace the heads with their positions, too:
MapIndexed[#2 &, listified, {-1}, Heads -> True]
The positions are given in the same format as used by Extract
.
Thus, mapping Extract
on the indices given by MapIndexed
re-constructs the original expression:
MapIndexed[Extract[expr, #2] &, listified, {-1}, Heads -> True]