MLX subsetting is like base R with a few differences:
Arguments
- x
An mlx array, or an R array/matrix/vector that will be converted via
as_mlx().- ...
Indices for each dimension. Provide one per axis; omitted indices select the full extent. Logical indices recycle to the dimension length.
- value
Value to assign, typically an mlx or R array. Values recycle in R's column-major order; their length must divide the selection size.
- drop
Should dimensions be dropped? (default: FALSE)
Details
drop = FALSEby default.Indices containing
NA, zero, or out-of-bounds positions give an error.Numeric indices truncate toward zero, as in R. Per-axis numeric indices must be undimensioned vectors; numeric matrices are coordinate indices.
Logical per-axis indices on matrices and arrays must not exceed the axis length.
Explicit
NULLindices select no elements; omitted indices select the full axis.Single indices on a 2D or higher array are only allowed for assignment. For example, if
xis a matrix,x[x < 0] <- 0is OK butsubset <- x[x < 0]is not. Usemlx_flatten()explicitly for subsetting.There is one exception: as in R, a single numeric matrix index selects individual elements. The number of columns must match the rank of
x; each row gives coordinates for one element. The return value from subsetting is a flat mlx vector.mlxvectors, logical masks, and matrices behave the same as their R equivalents.Duplicate vector assignments like
x[c(1,1)] <- 2:3are undefined behaviour. Coordinate matrix assignments reject duplicate rows.Character indices match against the relevant axis dimnames.
Examples
x <- mlx_matrix(1:9, 3, 3)
x[1, ]
#> mlx array [1 x 3]
#> dtype: float32
#> values:
#> [,1] [,2] [,3]
#> [1,] 1 4 7