Multik 0.3.0 Help

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.

val a = mk.ndarray(mk[1, 2, 3]) a[2] // 3 val b = mk.ndarray(mk[mk[1.5, 2.1, 3.0], mk[4.0, 5.0, 6.0]]) b[1, 2] // 6.0

When you index a single axis with an Int, the resulting array has one less dimension.

val row = b[1] // D1 array: [4.0, 5.0, 6.0]

Slicing with ranges

Use Kotlin ranges to take slices. Ranges are inclusive, and you can use ..< (or until) for exclusive end bounds.

val c = mk.ndarray(mk[10, 20, 30, 40, 50]) c[1..3] // [20, 30, 40] c[0..<3] // [10, 20, 30]

You can also specify a step using the .. operator again:

c[0..4..2] // [10, 30, 50]

Slicing multiple axes

For multidimensional arrays, pass a range per axis. If you omit axes, Multik treats them as full slices.

// take rows 0 and 1 from column 1 b[0..<2, 1] // [2.1, 5.0] // same as selecting the entire second axis b[1] // [4.0, 5.0, 6.0] b[1, 0..2..1] // [4.0, 5.0, 6.0]

Open-ended slices with sl

For open-ended slices, use sl helpers:

  • sl.first..k - from the start to k

  • k..sl.last - from k to the end

  • sl.bounds - the whole axis

val col = b[sl.bounds, 1] // full rows, column 1 val head = c[sl.first..2] // [10, 20, 30] val tail = c[2..sl.last] // [30, 40, 50]

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.

val keepDim = b[1..1] // D2 array with shape (1, 3)

Slicing by axis and ND arrays

When you need to slice an arbitrary axis or a general ND array, use slice:

val d = mk.ndarray(mk[mk[1, 2, 3], mk[4, 5, 6]]) val byAxis = d.slice<Int, D2, D2>(0..1, axis = 1) // take columns 0..1

For ND arrays, you can provide a map of axis to slice or index:

val e = mk.d3array(2, 3, 4) { it } val view = e.slice<Int, D3, D2>(mapOf(0 to 0..1.r, 2 to (0..2).toSlice())) // axis 0 is indexed, axis 2 is sliced, axis 1 is kept as full slice

Views vs copies

Slicing returns a view backed by the original data. If you need an independent copy, see Copies and views.

19 February 2026