BlockMap
Details
- In BlockMap[f,list,n], all sublists passed to f are of length n. Some elements at the end of list may not be visited.
- In BlockMap[f,list,n,1], all elements of list appear in at least one sublist.
- In BlockMap[f,list,n,d], if d is greater than n, some elements in the middle of list are skipped. »
- BlockMap can be used on SparseArray objects.
Examples
open all close allBasic Examples (4)
Apply a function to all non-overlapping, length-2 sublists:
BlockMap[f, Range[10], 2]Apply a function to overlapping sublists of length 2 with offset 1:
BlockMap[f, Range[10], 2, 1]BlockMap[f, (| | | | |
| ---- | ---- | ---- | ---- |
| 1 | 2 | 3 | 4 |
| 10 | 20 | 30 | 40 |
| 100 | 200 | 300 | 400 |
| 1000 | 2000 | 3000 | 4000 |), 2];Map[MatrixForm, %, {2}]Apply a function to all 2x2 sub-matrices:
BlockMap[f, (| | | | |
| ---- | ---- | ---- | ---- |
| 1 | 2 | 3 | 4 |
| 10 | 20 | 30 | 40 |
| 100 | 200 | 300 | 400 |
| 1000 | 2000 | 3000 | 4000 |), {2, 2}];MatrixForm@Map[MatrixForm, %, {3}]Scope (8)
Apply a function to a ragged array:
BlockMap[f, {{1, 2, 3, 4}, {5, 6}, 7, {{"hello"}}}, 2]Apply a function to blocks of size {2,1,2} of a rank-3 array:
BlockMap[f, ArrayReshape[Range@48, {6, 2, 4}], {2, 1, 2}]Specify an offset {1,2} with a block size {2,2} to allow overlapping for rows but not for columns:
BlockMap[f, (| | | | |
| ---- | ---- | ---- | ---- |
| 1 | 2 | 3 | 4 |
| 10 | 20 | 30 | 40 |
| 100 | 200 | 300 | 400 |
| 1000 | 2000 | 3000 | 4000 |), {2, 2}, {1, 2}];MatrixForm@Map[MatrixForm, %, {3}]Multidimensional offsets are effectively coerced to match the block size specification:
BlockMap[f, (| | | | |
| ---- | ---- | ---- | ---- |
| 1 | 2 | 3 | 4 |
| 10 | 20 | 30 | 40 |
| 100 | 200 | 300 | 400 |
| 1000 | 2000 | 3000 | 4000 |), 2, {1, 2}];
MatrixForm@Map[MatrixForm, %, {3}]Skip elements by using an offset larger than the block size:
BlockMap[f, {1, 2, 3, 4, 5, 6, 7, 8}, 2, 3]Incomplete sublists at the end are dropped:
BlockMap[f, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 4]The head need not be List:
BlockMap[f, h[1, 2, 3, 4], 2]BlockMap[f, h[h[1, 2, 3], h[4, 5, 6]], {2, 2}, 1]BlockMap works with SparseArray objects:
BlockMap[f, SparseArray[{i_, i_} -> 1, {6, 6}], {2, 2}]MatrixForm@Map[MatrixForm, %, {3}]Applications (5)
Compute successive differences of elements:
BlockMap[Last[#] - First[#]&, {a, b, c, d, e, f}, 2, 1]Compute a moving average with runs of 3 elements:
BlockMap[Mean, {a, b, c, d, e}, 3, 1]Compute a moving median of a matrix:
BlockMap[Median, {{1, 2}, {5, 3}, {1, 4}, {3, 2}, {5, 5}}, 2, 1]Compute a moving quantile for some data:
data = RandomFunction[WienerProcess[], {0, 10, 0.1}]["Values"];mq = BlockMap[Quantile[#, 0.75]&, data, 3, 1];ListLinePlot[{Rest@data, mq}]Smooth a simulated particle trajectory:
n[] := RandomReal[CauchyDistribution[0, 1]]f[] := {u Cos[u] + n[], u Sin[u] + n[]};data = Table[f[], {u, 0, 6π, 1 / 100}];The underlying signal and simulated path with noise:
{ParametricPlot[{u Cos[u], u Sin[u]}, {u, 0, 6π}], pd = ListPlot[data, AspectRatio -> 1]}Smooth the trajectory using a moving TrimmedMean:
smooth[r_] := BlockMap[TrimmedMean, data, r, 1]Increasing the window size gives a smoother trajectory:
Table[ListPlot[smooth[r], AspectRatio -> 1], {r, {25, 50, 100}}]Properties & Relations (2)
BlockMap is effectively the same as using Map and Partition:
BlockMap[f, Range[100], 4, 1] === Map[f, Partition[Range[100], 4, 1]]BlockMap need not construct all the sublists and requires less memory:
x = Range[10 ^ 4];
ByteCount[x]MemoryConstrained[BlockMap[Total, x, 4];, 10 ^ 5]MemoryConstrained[Map[Total, Partition[x, 4]];, 10 ^ 5]ListCorrelate[ker,list] effectively combines ker with a sliding block in list:
ker = {x, y};
list = Range[10];ListCorrelate[ker, list]Use an offset of 1 in BlockMap to apply the function to overlapping segments the length of ker:
BlockMap[ker.#&, list, Length[ker], 1]ListConvolve is similar except the kernel is reversed:
ListConvolve[ker, list]BlockMap[Reverse[ker].#&, list, Length[ker], 1]Related Guides
History
Text
Wolfram Research (2015), BlockMap, Wolfram Language function, https://reference.wolfram.com/language/ref/BlockMap.html.
CMS
Wolfram Language. 2015. "BlockMap." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/BlockMap.html.
APA
Wolfram Language. (2015). BlockMap. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/BlockMap.html
BibTeX
@misc{reference.wolfram_2026_blockmap, author="Wolfram Research", title="{BlockMap}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/BlockMap.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_blockmap, organization={Wolfram Research}, title={BlockMap}, year={2015}, url={https://reference.wolfram.com/language/ref/BlockMap.html}, note=[Accessed: 12-June-2026]}