Mirrors mlx.core.take_along_axis()
while accepting 1-based R indices.
Value
An mlx array. Names on the indexed axis are dropped because
per-position indices may reorder each slice differently.
Details
If y <- mlx_take_along_axis(x, idx, axis) where x is an m x n matrix
and idx is a matrix:
ywill have the same shape asidx, possibly afteridxhas been broadcast to the dimensions ofyfor all axes exceptaxis.For
axis = 1, values ofidxgive the row, and columns are in order:y[i, j]equalsx[idx[i, j], j].idxmust have 1 orncolumns.ywill have the same number of rows asidx.For
axis = 2, values ofidxgive the column, and rows are in order:y[i, j]equalsx[i, idx[i, j]].idxmust have 1 ormrows, andywill have the same number of columns asidx.
More generally, for x and idx of d dimensions, and axis = a:
y[i_1, ...., i_d]equalsx[i_1, ..., idx[i_1,...,i_d], ..., i_d]where theidxvector is in positiona.
For broadcasting, the simplest rule is that if idx has 1 column,
mlx_take_along_axis(x, idx, 1) is the same as x[drop(idx),]; and if
idx has 1 row, mlx_take_along_axis(x, idx, 2) is the same as
x[, drop(idx)].
Examples
x <- outer(1:3, c(0.1, 0.2), "+")
x <- as_mlx(x)
x
#> mlx array [3 x 2]
#> dtype: float32
#> values:
#> [,1] [,2]
#> [1,] 1.1 1.2
#> [2,] 2.1 2.2
#> [3,] 3.1 3.2
idx_cols <- matrix(c(1, 2,
2, 2,
1, 1), nrow = 3, byrow = TRUE)
mlx_take_along_axis(x, idx_cols, axis = 2)
#> mlx array [3 x 2]
#> dtype: float32
#> values:
#> [,1] [,2]
#> [1,] 1.1 1.2
#> [2,] 2.2 2.2
#> [3,] 3.1 3.1
idx_rows <- matrix(c(1, 2,
3, 1), nrow = 2, byrow = TRUE)
mlx_take_along_axis(x, idx_rows, axis = 1)
#> mlx array [2 x 2]
#> dtype: float32
#> values:
#> [,1] [,2]
#> [1,] 1.1 2.2
#> [2,] 3.1 1.2