Translates matrix indices by rows into indices by columns.
indexByRow(dim, idxs = NULL, ...)The current implementation does not support long-vector indices,
because both input and output indices are of type integers.
This means that the indices in argument idxs can only be in
range [1,2^31-1]. Using a greater value will be coerced to
NA_integer_. Moreover, returned indices can only be in the
same range [1,2^31-1].
dim <- c(5, 4)
X <- matrix(NA_integer_, nrow = dim[1], ncol = dim[2])
Y <- t(X)
idxs <- seq_along(X)
# Assign by columns
X[idxs] <- idxs
print(X)
#> [,1] [,2] [,3] [,4]
#> [1,] 1 6 11 16
#> [2,] 2 7 12 17
#> [3,] 3 8 13 18
#> [4,] 4 9 14 19
#> [5,] 5 10 15 20
# Assign by rows
Y[indexByRow(dim(Y), idxs)] <- idxs
print(Y)
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1 2 3 4 5
#> [2,] 6 7 8 9 10
#> [3,] 11 12 13 14 15
#> [4,] 16 17 18 19 20
stopifnot(X == t(Y))