({{, (RefLink[BlockMap, paclet:ref/BlockMap])[f,list,n] 
applies f to non-overlapping sublists of length n in list. }, {, (RefLink[BlockMap, paclet:ref/BlockMap])[f,list,n,d] 
applies f to sublists with offset d in list. }, {, (RefLink[BlockMap, paclet:ref/BlockMap])[f,list,{(Subscript[n, 1]),(Subscript[n, 2]),…},…] 
applies f to blocks of size (Subscript[n, 1])×(Subscript[n, 2])×….}})

OpenersNotesSection(Spacer1)Details

(RefLink[BlockMap, paclet:ref/BlockMap]) can be used on (RefLink[SparseArray, paclet:ref/SparseArray]) objects.

OpenersPrimaryExamplesSection(Spacer1)Examples  (18)

OpenersExampleSection0(Spacer1)Basic 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]

Apply a function to a matrix:

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}]

OpenersExampleSection1(Spacer1)Scope  (7)

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}]

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 (RefLink[List, paclet:ref/List]):

BlockMap[f,h[1,2,3,4],2]

BlockMap[f,h[h[1,2,3],h[4,5,6]],{2,2},1]

(RefLink[BlockMap, paclet:ref/BlockMap]) works with (RefLink[SparseArray, paclet:ref/SparseArray]) objects:

BlockMap[f,SparseArray[{i_,i_}->1,{6,6}],{2,2}]

MatrixForm@Map[MatrixForm,%,{3}]

OpenersExampleSection2(Spacer1)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 (RefLink[TrimmedMean, paclet:ref/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}}]

OpenersExampleSection3(Spacer1)Properties & Relations  (2)

(RefLink[BlockMap, paclet:ref/BlockMap]) is effectively the same as using (RefLink[Map, paclet:ref/Map]) and (RefLink[Partition, paclet:ref/Partition]):

BlockMap[f,Range[100],4,1]===Map[f,Partition[Range[100],4,1]]

(RefLink[BlockMap, paclet:ref/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]

(RefLink[ListCorrelate, paclet:ref/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 (RefLink[BlockMap, paclet:ref/BlockMap]) to apply the function to overlapping segments the length of ker:

BlockMap[ker.#&,list,Length[ker],1]

(RefLink[ListConvolve, paclet:ref/ListConvolve]) is similar except the kernel is reversed:

ListConvolve[ker,list]

BlockMap[Reverse[ker].#&,list,Length[ker],1]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ArrayFilter, paclet:ref/ArrayFilter])  ▪  (RefLink[Partition, paclet:ref/Partition])  ▪  (RefLink[Map, paclet:ref/Map])  ▪  (RefLink[SubsetMap, paclet:ref/SubsetMap])  ▪  (RefLink[MapIndexed, paclet:ref/MapIndexed])  ▪  (RefLink[ListCorrelate, paclet:ref/ListCorrelate])  ▪  (RefLink[MovingMap, paclet:ref/MovingMap])  ▪  (RefLink[CellularAutomaton, paclet:ref/CellularAutomaton])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Applying Functions to Lists, paclet:guide/ApplyingFunctionsToLists])}, {▪(RefLinkPlain[Handling Arrays of Data, paclet:guide/HandlingArraysOfData])}, {▪(RefLinkPlain[Functional Programming, paclet:guide/FunctionalProgramming])}})}})