Indexing and slicing
Overview
Indexing selects elements or subarrays, while slicing selects ranges along one or more axes. This section covers ranges, steps, open-ended slices, and how dimensionality changes.
Indexing basics
Multik uses zero-based indexing. Each index corresponds to an axis of the ndarray.
When you index a single axis with an Int, the resulting array has one less dimension.
Slicing with ranges
Use Kotlin ranges to take slices. Ranges are inclusive, and you can use ..< (or until) for exclusive end bounds.
You can also specify a step using the .. operator again:
Slicing multiple axes
For multidimensional arrays, pass a range per axis. If you omit axes, Multik treats them as full slices.
Open-ended slices with sl
For open-ended slices, use sl helpers:
sl.first..k- from the start tokk..sl.last- fromkto the endsl.bounds- the whole axis
Keeping or reducing dimensions
Indexing with an Int removes the corresponding axis. If you want to keep the axis, slice a range of length 1 instead.
Slicing by axis and ND arrays
When you need to slice an arbitrary axis or a general ND array, use slice:
For ND arrays, you can provide a map of axis to slice or index:
Views vs copies
Slicing returns a view backed by the original data. If you need an independent copy, see Copies and views.