mapSlices

mapSlices(A, dim, callback)

Try it yourself:

See also:
map

mapSlices(): Transforming Matrix Slices

mapSlices() applies a function to slices of a matrix along selected dimensions. It is useful when work should happen row by row, column by column, or across higher-dimensional blocks rather than entry by entry.

Choose dimensions carefully

Know which axis represents observations and which represents features before mapping slices. Check shape with size(), and compare with map(), which processes individual entries. For row or column extraction, use row() and column().

Slice functions should return compatible outputs if they will be assembled into a matrix. Use mean() for per-slice summaries or sum() for aggregation.

All functions