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
- drop
Should dimensions be dropped? (default: FALSE)
Details
drop = FALSEby default.Indices containing
NAgive an error.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 assignments like
x[c(1,1)] <- 2:3are undefined behaviour.Character indices are not supported as MLX has no concept of dimension names.
Examples
x <- mlx_matrix(1:9, 3, 3)
x[1, ]
#> mlx array [1 x 3]
#> dtype: float32
#> device: cpu
#> values:
#> [,1] [,2] [,3]
#> [1,] 1 4 7